在我的 log4j.properties 中我有:
log4j.rootLogger=DEBUG,stdout
log4j.logger.notRootLogger=DEBUG,somewhereelse
appender stdout 和 somewhereelse 都配置正确,stdout 写入控制台,somewhereelse 写入文件。
在每个类的代码中,我要么设置:
static Logger log = Logger.getLogger("notRootLogger);
^ 当我不想让内容进入控制台时。
-或-
static Logger log = Logger.getRootLogger();
^ 当我这样做的时候。
我必须在 log4.properties 中做什么才能阻止写入 notRootLogger 的内容最终出现在 stdout 中?根记录器写入的任何地方是否存在某种需要以某种方式关闭的继承?
我不想为我只想记录到控制台的每个类单独配置记录器。
最佳答案
您需要设置 additivity = false
,IIRC。来自log4j manual :
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.
试试这个:
log4j.rootLogger=DEBUG,stdout
log4j.logger.notRootLogger=DEBUG,somewhereelse
log4j.additivity.notRootLogger=false
关于java - log4j 将所有日志输出定向到标准输出,即使它不应该,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1244147/