如何使用 Simple Formatter 修改日志格式?
How do i modify a log format with Simple Formatter?
我尝试将此行添加到 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 is that quotes are only needed on the pattern if you are 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,该模式就会在 class 的生命周期内使用。使用 System.setProperty
仅当您在记录开始之前设置模式时才有效,因此不要依赖于在复杂程序中工作的路由。
我尝试将此行添加到 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 is that quotes are only needed on the pattern if you are 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,该模式就会在 class 的生命周期内使用。使用 System.setProperty
仅当您在记录开始之前设置模式时才有效,因此不要依赖于在复杂程序中工作的路由。