log4j 将所有日志输出定向到 stdout,即使它不应该这样做
在我的 log4j.properties 中,我有:
log4j.rootLogger=DEBUG,stdout
log4j.logger.notRootLogger=DEBUG,somewhereelse
追加器 stdout 和其他地方都配置正确,stdout 写入控制台,其他地方写入文件。
在每个类的代码中,我设置:
static Logger log = Logger.getLogger("notRootLogger);
^ 当我不想让东西进入控制台时。
-或-
static Logger log = Logger.getRootLogger();
^ 当我这样做时。
我必须在 log4.properties 中做什么才能阻止写入 notRootLogger 的内容最终出现在 stdout 中? 根记录器写入的地方是否存在某种需要以某种方式关闭的继承?
我不想为每个类单独配置一个记录器,我只想记录到控制台。
In my log4j.properties I have:
log4j.rootLogger=DEBUG,stdout
log4j.logger.notRootLogger=DEBUG,somewhereelse
The appenders stdout and somewhereelse are both configured properly, stdout writes to the console and somewhereelse writes to a file.
In my code in each class either I set either:
static Logger log = Logger.getLogger("notRootLogger);
^ When I don't want stuff going to the console.
-OR-
static Logger log = Logger.getRootLogger();
^ When I do.
What do I have to do in log4.properties to stop the things that are written to notRootLogger ending up in stdout? Is there some sort of inheritance of wherever the root logger writes to going on that needs to be turned off somehow?
I don't want to have to configure a logger for every single class individually that I just want to log to the console.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果java类中已经定义了logger的属性,那么最后可以调用logger.shutdown()方法,最好是在finally块中,以禁止logger的附加性。
If the properties of logger have been defined in java class, you can call logger.shutdown() method in the end, preferebly in the finally block to prohibit the additive nature of logger.
您需要设置
additivity = false
,IIRC。 来自 log4j 手册:尝试这个:
You need to set
additivity = false
, IIRC. From the log4j manual:Try this:
嗯,应该更仔细地阅读 log4j 的简短介绍来
修复它,因为它继承了层次结构中位于其上方的记录器的附加程序,并且根记录器显然位于层次结构的顶部。
Hmm, should have read the short intro to log4j more carefully
fixes it, because it inherits appenders from the loggers above it in the hierarchy, and the root logger is at the top of the hierarchy obviously.