java.util.logging.Formatter类的使用及代码示例

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

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

Formatter介绍

[英]Formatter objects are used to format LogRecord objects into a string representation. Head and tail strings are sometimes used to wrap a set of records. The getHead and getTail methods are used for this purpose.
[中]格式化程序对象用于将日志记录对象格式化为字符串表示形式。头字符串和尾字符串有时用于包装一组记录。getHead和getTail方法用于此目的。

代码示例

代码示例来源: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: 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: stackoverflow.com

StringBuilder sb = new StringBuilder();
sb.append(new Date(record.getMillis()))
  .append(" ")
  .append(record.getLevel().getLocalizedName())
  .append(": ")
  .append(formatMessage(record))
  .append(LINE_SEPARATOR);
if (record.getThrown() != null) {
  try {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    record.getThrown().printStackTrace(pw);
    pw.close();
    sb.append(sw.toString());
  } catch (Exception ex) {

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

private synchronized void flushAndClose() throws SecurityException {
 if (writer != null) {
  try {
   if (!doneHeader) {
    writer.write(getFormatter().getHead(this));
    doneHeader = true;
   }
   writer.write(getFormatter().getTail(this));
   writer.flush();
   writer.close();
  } 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.CLOSE_FAILURE);
  }
  writer = null;
 }
}

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

protected void closeWriter() {
  
  writerLock.writeLock().lock();
  try {
    if (writer == null)
      return;
    writer.write(getFormatter().getTail(this));
    writer.flush();
    writer.close();
    writer = null;
    date = "";
  } catch (Exception e) {
    reportError(null, e, ErrorManager.CLOSE_FAILURE);
  } finally {
    writerLock.writeLock().unlock();
  }
}

代码示例来源: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: camunda/camunda-bpm-platform

FileOutputStream fos = new FileOutputStream(pathname, true);
  OutputStream os = bufferSize>0?new BufferedOutputStream(fos,bufferSize):fos;
  writer = new PrintWriter(
      (encoding != null) ? new OutputStreamWriter(os, encoding)
                : new OutputStreamWriter(os), false);
  writer.write(getFormatter().getHead(this));
} catch (Exception e) {
  reportError(null, e, ErrorManager.OPEN_FAILURE);

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

result = getFormatter().format(record);
} catch (Exception e) {
  reportError(null, e, ErrorManager.FORMAT_FAILURE);
    writer.write(result);
    if (bufferSize < 0) {
      writer.flush();

代码示例来源:origin: javapathfinder/jpf-core

@Override
public void publish (LogRecord r) {
 String msg = getFormatter().format(r);
 out.println(msg);
}

代码示例来源: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: 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: 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: camunda/camunda-bpm-platform

/**
 * Formats message for the log record. This method removes any fully
 * qualified throwable class names from the message.
 *
 * @param record the log record.
 * @return the formatted message string.
 */
@Override
public String formatMessage(final LogRecord record) {
  String msg = super.formatMessage(record);
  msg = replaceClassName(msg, record.getThrown());
  msg = replaceClassName(msg, record.getParameters());
  return msg;
}

代码示例来源:origin: com.scireum/sirius-kernel

@Override
public void publish(LogRecord record) {
  repository.getLogger(record.getLoggerName() == null ? "unknown" : record.getLoggerName())
       .log(Log.convertJuliLevel(record.getLevel()),
          formatter.formatMessage(record),
          record.getThrown());
}

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

private String getMessage(final LogRecord record) {
 StringBuilder stringBuilder = new StringBuilder();
 stringBuilder.append('(').append("tid=").append(record.getThreadID())
   .append(" msgId=").append(record.getSequenceNumber()).append(") ");
 if (record.getMessage() != null) {
  stringBuilder.append(getFormatter().formatMessage(record));
 }
 return stringBuilder.toString();
}

代码示例来源:origin: org.codehaus.mojo/jaxb2-maven-plugin

/**
 * {@inheritDoc}
 */
@Override
public void publish(final LogRecord record) {
  if (this.isLoggable(record)) {
    final Level level = record.getLevel();
    final String message = prefix + getFormatter().format(record);
    if (Level.SEVERE.equals(level)) {
      log.error(message);
    } else if (Level.WARNING.equals(level)) {
      log.warn(message);
    } else if (Level.INFO.equals(level)) {
      log.info(message);
    } else {
      log.debug(message);
    }
  }
}

代码示例来源: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: 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: org.wso2.carbon/org.wso2.carbon.bootstrap

public static LogRecord formatMessage(Formatter formatter, LogRecord record) {
  record.setMessage(formatter.formatMessage(record));
  record.setSourceClassName(record.getSourceClassName());
  return record;
}

代码示例来源:origin: jenkinsci/jenkins-test-harness

@Override
  public synchronized void publish(LogRecord record) {
    super.publish(record);
    String message = f.formatMessage(record);
    Throwable x = record.getThrown();
    messages.add(message == null && x != null ? x.toString() : message);
  }
};

相关文章