将错误记录到stderr并使用log4j将信息记录到stdout [英] Logging error to stderr and debug, info to stdout with log4j

查看:302
本文介绍了将错误记录到stderr并使用log4j将信息记录到stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用apache log4j将日志记录添加到我正在开发的应用程序中。此时,我想将所有日志消息重定向到级别INFO和更低(TRACE,DEBUG)到stdout以及所有其他日志消息从WARN及以上(ERROR,FATAL)到stderr。
例如:

I want to add logging to an application I am developing, using apache log4j. At this point I want to redirect all log messages for level INFO and lower (TRACE, DEBUG) to stdout and all other log messages from WARN and above (ERROR, FATAL) to stderr. For example:

...
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文件应该是什么?在这里看看这个样子:

So what should be my log4j.properties file? Here how it looks at this momment:

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打印。

The problem with the above configuration is that the logger.error() ... is printed at stdout too.

推荐答案

Per Jon Skeet上一篇文章发布

Per Jon Skeet's previous post at Post


遗憾的是,没有最大阈值,所以无论你在哪里获得
调试消息,你都会收到警告信息。这有点痛苦,
IMO。

unfortunately there is no maximum threshold, so wherever you get debug messages, you also get warning messages. That's a bit of a pain, IMO.

这篇关于将错误记录到stderr并使用log4j将信息记录到stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆