我想使用apache log4j将日志记录添加到正在开发的应用程序中。在这一点上,我想将所有级别为INFO或更低级别(TRACE,DEBUG)的日志消息重定向到stdout,并将所有其他日志消息从WARN及更高版本(ERROR,FATAL)重定向到stderr。
例如:
...
logger.info("Processing at some point"); // must be written to stdout
logger.debug("Point x was processed"); // must be written to stdout
logger.warn("Incorrect point config"); // must be written only to stderr
logger.error("Exception occurred at point x"); // must be written only to stderr
那么我的log4j.properties文件应该是什么?这是如何看待这一时刻的:
log4j.rootLogger=DEBUG, stdout, stderr
# configure stdout
# set the conversion pattern of stdout
# Print the date in ISO 8601 format
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold = DEBUG
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %-5p %d [%t][%F:%L] : %m%n
# configure stderr
# set the conversion pattern of stdout
# Print the date in ISO 8601 format
log4j.appender.stderr = org.apache.log4j.ConsoleAppender
log4j.appender.stderr.Threshold = WARN
log4j.appender.stderr.Target = System.err
log4j.appender.stderr.layout = org.apache.log4j.PatternLayout
log4j.appender.stderr.layout.ConversionPattern = %-5p %d [%t][%F:%L] : %m%n
上面配置的问题是logger.error()...也被打印在stdout上。
最佳答案
Per Jon Skeet在Post上的先前文章