如何让 java 日志记录输出显示在一行上?

2022-08-31 07:50:40

目前,默认条目如下所示:

Oct 12, 2008 9:45:18 AM myClassInfoHere
INFO: MyLogMessageHere

如何执行此操作?

Oct 12, 2008 9:45:18 AM myClassInfoHere - INFO: MyLogMessageHere

澄清我正在使用java.util.logging


答案 1

从Java 7开始,java.util.logging.SimpleFormatter支持从系统属性获取其格式,因此将类似这样的东西添加到JVM命令行将导致它在一行上打印:

-Djava.util.logging.SimpleFormatter.format='%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n'

或者,您也可以将其添加到您的:logger.properties

java.util.logging.SimpleFormatter.format='%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n'

答案 2

1)-Djava.util.logging.SimpleFormatter.format

Java 7 支持具有格式字符串语法的属性。java.util.Formatter

-Djava.util.logging.SimpleFormatter.format=... 

请参阅此处

我最喜欢的是:

-Djava.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %2$s %5$s%6$s%n

这使得输出像:

2014-09-02 16:44:57 SEVERE org.jboss.windup.util.ZipUtil unzip: Failed to load: foo.zip

2) 将其放入 IDE

IDE 通常允许您为项目设置系统属性。例如,在 NetBeans 中,而不是添加 -D...=...在某个位置,以 - 不带任何引号的形式在操作对话框中添加属性。IDE 应该弄清楚。java.util.logging.SimpleFormatter.format=%1$tY-%1$tm-...

3)把它放到Maven - Surefire

为了您的方便,以下是如何将其放入Surefire:

<!-- Surefire -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.17</version>
    <configuration>
        <systemPropertyVariables>
            <!-- Set JUL Formatting -->
            <java.util.logging.SimpleFormatter.format>%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %2$s %5$s%6$s%n</java.util.logging.SimpleFormatter.format>
        </systemPropertyVariables>
    </configuration>
</plugin>

4)手工制作

我有一个库,里面有几个java.util.logging相关的类。其中,它是.可下载的罐子在这里SingleLineFormatter

public class SingleLineFormatter extends Formatter {

  Date dat = new Date();
  private final static String format = "{0,date} {0,time}";
  private MessageFormat formatter;
  private Object args[] = new Object[1];

  // Line separator string.  This is the value of the line.separator
  // property at the moment that the SimpleFormatter was created.
  //private String lineSeparator = (String) java.security.AccessController.doPrivileged(
  //        new sun.security.action.GetPropertyAction("line.separator"));
  private String lineSeparator = "\n";

  /**
   * Format the given LogRecord.
   * @param record the log record to be formatted.
   * @return a formatted log record
   */
  public synchronized String format(LogRecord record) {

    StringBuilder sb = new StringBuilder();

    // Minimize memory allocations here.
    dat.setTime(record.getMillis());    
    args[0] = dat;


    // Date and time 
    StringBuffer text = new StringBuffer();
    if (formatter == null) {
      formatter = new MessageFormat(format);
    }
    formatter.format(args, text, null);
    sb.append(text);
    sb.append(" ");


    // Class name 
    if (record.getSourceClassName() != null) {
      sb.append(record.getSourceClassName());
    } else {
      sb.append(record.getLoggerName());
    }

    // Method name 
    if (record.getSourceMethodName() != null) {
      sb.append(" ");
      sb.append(record.getSourceMethodName());
    }
    sb.append(" - "); // lineSeparator



    String message = formatMessage(record);

    // Level
    sb.append(record.getLevel().getLocalizedName());
    sb.append(": ");

    // Indent - the more serious, the more indented.
    //sb.append( String.format("% ""s") );
    int iOffset = (1000 - record.getLevel().intValue()) / 100;
    for( int i = 0; i < iOffset;  i++ ){
      sb.append(" ");
    }


    sb.append(message);
    sb.append(lineSeparator);
    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) {
      }
    }
    return sb.toString();
  }
}

推荐