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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…