java - 如何使用 Simple Formatter 修改日志格式?

标签 java logging formatter java.util.logging

我尝试将此行添加到logging.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'

我也尝试了System.setProperty,但它仍然不起作用,我做错了什么?

import java.util.*;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.io.*;
import java.awt.*;

public class LoggerFormat
{
private static final Logger logger = Logger.getLogger(LoggerFormat.class.getName());

public static void main(String[] args)
{
    System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tF %1$tT %4$s %2$s %1$tL");
    SimpleFormatter sf = new SimpleFormatter();
    System.out.println("-- main method starts --");
    logger.info("in LoggerFormat");
    logger.warning("a test warning");
}
}

最佳答案

这里可能会出现很多问题。首先确保您运行的 Java 版本 (7 b138) 或更高版本已修复 JDK-6381464 : SimpleFormatter should use one single line format .

documentation 中没有解释的一件事如果您是setting the pattern via the command line,则仅在模式上需要引号。并且该模式包含一个空白字符。

因此,如果您在logging.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

如果您将格式设置为系统属性,则必须在启动时设置它:

-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"

接下来您要做的就是使用测试程序来验证您的模式是否可以编译。如果模式语法错误,SimpleFormatter 将回退到默认模式。这是一个示例测试程序:

public static void main(String[] args) throws Exception {
    final String format = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n";
    final String key = "java.util.logging.SimpleFormatter.format";
    test(format);
    test(System.getProperty(key, format));
    test(LogManager.getLogManager().getProperty(key));
    test(new SimpleFormatter());
}

private static void test(Formatter f) {
    LogRecord record = newLogRecord();
    System.out.println(f.format(record));
}

private static LogRecord newLogRecord() {
    LogRecord r = new LogRecord(Level.INFO, "Message");
    r.setSourceClassName("sourceClassName");
    r.setSourceMethodName("sourceMethodName");
    r.setLoggerName("loggerName");
    r.setThrown(new Throwable("thrown"));
    return r;
}

private static void test(String format) {
    if (format != null) {
        LogRecord record = newLogRecord();
        Throwable t = record.getThrown();
        System.out.println(String.format(format,
                new java.util.Date(record.getMillis()),
                record.getSourceClassName(),
                record.getLoggerName(),
                record.getLevel().getLocalizedName(),
                record.getMessage(),
                t != null ? t.toString() : ""));
        //TODO: Place printStackTrace into a string.
    } else {
        System.out.println("Format is null.");
    }
}

最后,该格式只能在启动时设置一次。一旦 SimpleFormatter 被加载,该模式就会在类的生命周期中使用。仅当您在日志记录开始之前设置模式时,使用 System.setProperty 才会起作用,因此不要依赖于在复杂程序中工作的路由。

关于java - 如何使用 Simple Formatter 修改日志格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49186029/

相关文章:

java - Spring 3.2 MVC - 无法将 URL 与 Controller 匹配

java - 在 Eclipse IDE 中自定义代码格式样式

java - 我应该为此动画使用 SwingWorker、线程或递归更新吗?

Java,用数据填充Object[][]数组

logging - Cygwin 上的 syslog 输出存储在哪里?

java - 如何使用带有 log4j2 的 Java 日志记录来正确管理使用插件创建的日志文件的生命周期

java - 智能 Java 格式化程序

java - 如何转换字符串2019-03-29T10 :45-05:00[America/Chicago] to localdate?

java - 在 Java 或 Perl 中创建随机 XML 条目

python - 在多个模块中使用日志记录