java.util.logging.Formatter.format()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(165)

本文整理了Java中java.util.logging.Formatter.format()方法的一些代码示例,展示了Formatter.format()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Formatter.format()方法的具体详情如下:
包路径:java.util.logging.Formatter
类名称:Formatter
方法名:format

Formatter.format介绍

[英]Converts a LogRecord object into a string representation. The resulted string is usually localized and includes the message field of the record.
[中]将LogRecord对象转换为字符串表示形式。结果字符串通常是本地化的,并包括记录的消息字段。

代码示例

代码示例来源:origin: pmd/pmd

@Override
public void publish(LogRecord logRecord) {
  System.out.println(FORMATTER.format(logRecord));
  if (logRecord.getThrown() != null) {
    // Use the same channel, to make sure that the stacktrace comes
    // after the message on the console (using printStackTrace
    // directly messes things up)
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter, true);
    logRecord.getThrown().printStackTrace(printWriter);
    System.out.println(stringWriter.toString());
  }
}

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

@Override
  public void publish(LogRecord record) {

    int level = getAndroidLevel(record.getLevel());
//        String tag = loggerNameToTag(record.getLoggerName());
    String tag = record.getLoggerName();

    try {
      String message = JME_FORMATTER.format(record);
      Log.println(level, tag, message);
    } catch (RuntimeException e) {
      Log.e("AndroidHandler", "Error logging message.", e);
    }
  }

代码示例来源:origin: iBotPeaches/Apktool

@Override
public void publish(LogRecord record) {
  if (getFormatter() == null) {
    setFormatter(new SimpleFormatter());
  }
  try {
    String message = getFormatter().format(record);
    if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
      System.err.write(message.getBytes());
    } else {
      if (record.getLevel().intValue() >= Level.INFO.intValue()) {
        System.out.write(message.getBytes());
      } else {
        if (verbosity == Verbosity.VERBOSE) {
          System.out.write(message.getBytes());
        }
      }
    }
  } catch (Exception exception) {
    reportError(null, exception, ErrorManager.FORMAT_FAILURE);
  }
}
@Override

代码示例来源:origin: traccar/traccar

@Override
public synchronized void publish(LogRecord record) {
  if (isLoggable(record)) {
    try {
      String suffix = "";
      if (rotate) {
        suffix = new SimpleDateFormat("yyyyMMdd").format(new Date(record.getMillis()));
        if (writer != null && !suffix.equals(this.suffix)) {
          writer.close();
          writer = null;
          if (!new File(name).renameTo(new File(name + "." + this.suffix))) {
            throw new RuntimeException("Log file renaming failed");
          }
        }
      }
      if (writer == null) {
        this.suffix = suffix;
        writer = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream(name, true), StandardCharsets.UTF_8));
      }
      writer.write(getFormatter().format(record));
      writer.flush();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}

代码示例来源:origin: pmd/pmd

@Override
public void publish(LogRecord logRecord) {
  // Map the log levels from java.util.logging to Ant
  int antLevel;
  Level level = logRecord.getLevel();
  if (level == Level.FINEST) {
    antLevel = Project.MSG_DEBUG; // Shown when -debug is supplied to
    // Ant
  } else if (level == Level.FINE || level == Level.FINER || level == Level.CONFIG) {
    antLevel = Project.MSG_VERBOSE; // Shown when -verbose is supplied
    // to Ant
  } else if (level == Level.INFO) {
    antLevel = Project.MSG_INFO; // Always shown
  } else if (level == Level.WARNING) {
    antLevel = Project.MSG_WARN; // Always shown
  } else if (level == Level.SEVERE) {
    antLevel = Project.MSG_ERR; // Always shown
  } else {
    throw new IllegalStateException("Unknown logging level"); // shouldn't
    // get ALL
    // or NONE
  }
  project.log(FORMATTER.format(logRecord), antLevel);
  if (logRecord.getThrown() != null) {
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter, true);
    logRecord.getThrown().printStackTrace(printWriter);
    project.log(stringWriter.toString(), antLevel);
  }
}

代码示例来源:origin: chewiebug/GCViewer

/**
 * @see java.util.logging.Handler#publish(java.util.logging.LogRecord)
 */
public void publish(LogRecord record) {
  try {
    if (isLoggable(record)) {
      final int level = record.getLevel().intValue();
      if (level >=Level.WARNING.intValue() && level < Level.OFF.intValue()) {
        ++errorCount;
      }
      if (!hasErrors) {
        hasErrors = level >= Level.WARNING.intValue() && level < Level.OFF.intValue();
      }
      try {
        String formattedRecord = getFormatter().format(record);
        textArea.append(formattedRecord);
      } 
      catch (RuntimeException e) {
        reportError(e.toString(), e, ErrorManager.WRITE_FAILURE);
      }
    }
  }
  catch (Exception e) {
    reportError(e.toString(), e, ErrorManager.GENERIC_FAILURE);
  }
}

代码示例来源:origin: apache/geode

@Override
public void publish(LogRecord record) {
 CommandResponseWriter responseWriter =
   CommandExecutionContext.getAndCreateIfAbsentCommandResponseWriter();
 responseWriter.println(getFormatter().format(record));
}

代码示例来源:origin: 4thline/cling

String message = getFormatter().format(record);
  Log.println(level, tag, message);
} catch (RuntimeException e) {

代码示例来源:origin: googleapis/google-cloud-java

private LogEntry logEntryFor(LogRecord record) throws Exception {
 String payload = getFormatter().format(record);
 Level level = record.getLevel();
 LogEntry.Builder builder =
   LogEntry.newBuilder(Payload.StringPayload.of(payload))
     .setTimestamp(record.getMillis())
     .setSeverity(severityFor(level));
 if (!baseLevel.equals(level)) {
  builder
    .addLabel("levelName", level.getName())
    .addLabel("levelValue", String.valueOf(level.intValue()));
 }
 for (LoggingEnhancer enhancer : enhancers) {
  enhancer.enhanceLogEntry(builder);
 }
 return builder.build();
}

代码示例来源:origin: org.postgresql/postgresql

public synchronized void publish(final LogRecord record) {
 if (!isLoggable(record)) {
  return;
 }
 String msg;
 try {
  msg = getFormatter().format(record);
 } catch (Exception ex) {
  // We don't want to throw an exception here, but we
  // report the exception to any registered ErrorManager.
  reportError(null, ex, ErrorManager.FORMAT_FAILURE);
  return;
 }
 try {
  if (!doneHeader) {
   writer.write(getFormatter().getHead(this));
   doneHeader = true;
  }
  writer.write(msg);
 } catch (Exception ex) {
  // We don't want to throw an exception here, but we
  // report the exception to any registered ErrorManager.
  reportError(null, ex, ErrorManager.WRITE_FAILURE);
 }
}

代码示例来源:origin: google/guava

assertThat(logFormatter.format(record)).doesNotContain("NoOpService");

代码示例来源:origin: robovm/robovm

msg = getFormatter().format(record);
} catch (Exception e) {
  getErrorManager().error("Exception occurred when formatting the log record",

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testReportFormatError() {
 expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
 expect(options.getService()).andReturn(logging);
 logging.setFlushSeverity(Severity.ERROR);
 expectLastCall().once();
 logging.setWriteSynchronicity(Synchronicity.ASYNC);
 expectLastCall().once();
 replay(options, logging);
 Formatter formatter = EasyMock.createStrictMock(Formatter.class);
 RuntimeException ex = new RuntimeException();
 ErrorManager errorManager = EasyMock.createStrictMock(ErrorManager.class);
 errorManager.error(null, ex, ErrorManager.FORMAT_FAILURE);
 expectLastCall().once();
 LogRecord record = newLogRecord(Level.FINEST, MESSAGE);
 expect(formatter.format(record)).andThrow(ex);
 replay(errorManager, formatter);
 Handler handler = new LoggingHandler(LOG_NAME, options, DEFAULT_RESOURCE);
 handler.setLevel(Level.ALL);
 handler.setErrorManager(errorManager);
 handler.setFormatter(formatter);
 handler.publish(record);
 verify(errorManager, formatter);
}

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * Creates the formatted log record or reports a formatting error.
 * @param f the formatter.
 * @param r the log record.
 * @return the formatted string or an empty string.
 */
private String format(final Formatter f, final LogRecord r) {
  try {
    return f.format(r);
  } catch (final RuntimeException RE) {
    reportError(RE.getMessage(), RE, ErrorManager.FORMAT_FAILURE);
    return "";
  }
}

代码示例来源:origin: com.sun.mail/javax.mail

/**
 * Creates the formatted log record or reports a formatting error.
 * @param f the formatter.
 * @param r the log record.
 * @return the formatted string or an empty string.
 */
private String format(final Formatter f, final LogRecord r) {
  try {
    return f.format(r);
  } catch (final RuntimeException RE) {
    reportError(RE.getMessage(), RE, ErrorManager.FORMAT_FAILURE);
    return "";
  }
}

代码示例来源:origin: com.google.gwt/gwt-servlet

@Override
public void publish(LogRecord record) {
 if (!isSupported() || !isLoggable(record)) {
  return;
 }
 String msg = getFormatter().format(record);
 int val = record.getLevel().intValue();
 if (val <= Level.WARNING.intValue()) {
  System.out.println(msg);
 } else {
  System.err.println(msg);
 }
}

代码示例来源:origin: com.google.gwt/gwt-servlet

@Override
public void publish(LogRecord record) {
 if (!isSupported() || !isLoggable(record)) {
  return;
 }
 String msg = getFormatter().format(record);
 GWT.log(msg, record.getThrown());
}

代码示例来源:origin: SpigotMC/BungeeCord

@Override
public void publish(LogRecord record)
{
  if ( isLoggable( record ) )
  {
    print( getFormatter().format( record ) );
  }
}

代码示例来源:origin: com.google.gwt/gwt-servlet

@Override
public void publish(LogRecord record) {
 if (!isSupported() || !isLoggable(record)) {
  return;
 }
 String msg = getFormatter().format(record);
 int val = record.getLevel().intValue();
 if (val >= Level.SEVERE.intValue()) {
  error(msg);
 } else if (val >= Level.WARNING.intValue()) {
  warn(msg);
 } else if (val >= Level.INFO.intValue()) {
  info(msg);
 } else {
  log(msg);
 }
}

代码示例来源:origin: com.google.gwt/gwt-servlet

@Override
 @SuppressIsSafeHtmlCastCheck
 public void publish(LogRecord record) {
  if (!isLoggable(record)) {
   return;
  }
  Formatter formatter = getFormatter();
  String msg = formatter.format(record);
  // We want to make sure that unescaped messages are not output as HTML to
  // the window and the HtmlLogFormatter ensures this. If you want to write a
  // new formatter, subclass HtmlLogFormatter and override the getHtmlPrefix
  // and getHtmlSuffix methods.
  if (formatter instanceof HtmlLogFormatter) {
   widgetContainer.add(new HTML(msg));
  } else {
   widgetContainer.add(new Label(msg));
  }
 }
}

相关文章