java log4j 将所有日志输出定向到标准输出,即使它不应该
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1244147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4j directs all log output to stdout even though it's not supposed to
提问by cons
In my log4j.properties I have:
在我的 log4j.properties 中,我有:
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?
我必须在 log4.properties 中做什么才能阻止写入 notRootLogger 的内容最终出现在 stdout 中?是否存在某种需要以某种方式关闭根记录器写入的地方的继承?
I don't want to have to configure a logger for every single class individually that I just want to log to the console.
我不想为我只想登录到控制台的每个类单独配置记录器。
回答by Jon Skeet
You need to set additivity = false, IIRC. From the log4j manual:
您需要设置additivity = false,IIRC。从log4j 手册:
Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy. In other words, appenders are inherited additively from the logger hierarchy. For example, if a console appender is added to the root logger, then all enabled logging requests will at least print on the console. If in addition a file appender is added to a logger, say C, then enabled logging requests for C and C's children will print on a file and on the console. It is possible to override this default behavior so that appender accumulation is no longer additive by setting the additivity flag to false.
给定记录器的每个启用的日志记录请求都将转发到该记录器中的所有附加程序以及层次结构中更高的附加程序。换句话说,appender 是从 logger 层次结构中附加地继承的。例如,如果将控制台 appender 添加到根记录器,则所有启用的日志记录请求至少会打印在控制台上。如果另外将文件附加程序添加到记录器,例如 C,则启用的 C 和 C 子项的日志记录请求将打印在文件和控制台上。可以通过将 additivity 标志设置为 false 来覆盖此默认行为,以便 appender 累积不再是可加的。
Try this:
试试这个:
log4j.rootLogger=DEBUG,stdout
log4j.logger.notRootLogger=DEBUG,somewhereelse
log4j.additivity.notRootLogger=false
回答by cons
Hmm, should have read the short intro to log4j more carefully
嗯,应该更仔细地阅读 log4j 的简短介绍
log4j.additivity.notRootLogger=false
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.
修复它,因为它从层次结构中它上面的记录器继承了appender,并且根记录器显然位于层次结构的顶部。
回答by ashutosh5111
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.
如果在java类中已经定义了logger的属性,可以在最后调用logger.shutdown()方法,最好在finally块中,禁止logger的可加性。

