Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

java - How do i modify a log format with Simple Formatter?

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'

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");
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A bunch of things can go wrong here. First make sure you are running a version of Java (7 b138) or later 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.

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"

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...