如何使用Simple Formatter修改日志格式? [英] How do i modify a log format with Simple Formatter?

查看:1146
本文介绍了如何使用Simple Formatter修改日志格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试将此行添加到logging.properties中,但输出不会更改

I've tried adding this line into logging.properties, but the output doesn't change

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,但它仍然不起作用,我做错了什么?

I also tried System.setProperty, but it still doesn't work, what am i doing wrong?

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");
}
}


推荐答案

A一堆东​​西在这里可能会出问题。首先确保您运行的Java版本(7 b138)已修复 JDK- 6381464:SimpleFormatter应该使用一种单行格式

A bunch of things can go wrong here. First make sure you are running a version of Java (7 b138) that has fix for JDK-6381464 : SimpleFormatter should use one single line format.

文档中没有解释的一件事是,只有在模式中才需要引号通过命令行设置模式,模式包含空格字符。

One thing that is not explained in the documentation is that quotes are only needed on the pattern if you are setting the pattern via the command line and the pattern contains a whitespace character.

因此,如果您在logging.properties中设置格式,则删除引号:

So if you are setting the format in the logging.properties then drop the quotes:

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

如果要将格式设置为系统属性然后你必须在发布时设置它:

If you are setting the format as a system property then you have to set it on launch:

-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将回退到默认模式。下面是一个示例测试程序:

Next thing you want to do is use a test program to verify that your pattern compiles. If the pattern syntax is wrong the SimpleFormatter will fall back to the default pattern. Here is an example test program:

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");
    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 只有在开始记录之前设置模式才有效,所以不要依赖于在复杂程序中工作的路由。

Finally, the format can only be set one time on startup. As soon as the SimpleFormatter is loaded that pattern is used for the life of the class. Using System.setProperty will only work if you set the pattern before logging starts so don't depend on that route ever working in a complex program.

这篇关于如何使用Simple Formatter修改日志格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆