How do i modify a log format with Simple Formatter?
Asked Answered
F

1

1

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");
}
}
Feudatory answered 9/3, 2018 at 3:13 Comment(0)
S
2

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.

Spar answered 12/3, 2018 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.