java – log4j将所有日志输出引导到stdout,即使不是这样

前端之家收集整理的这篇文章主要介绍了java – log4j将所有日志输出引导到stdout,即使不是这样前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的log4j.properties我有:
log4j.rootLogger=DEBUG,stdout

log4j.logger.notRootLogger=DEBUG,somewhereelse

附加信号stdout和athereelse都配置正确,stdout写入控制台和某个文件的写入.

在我的每个类的代码中,我设置:

static Logger log =  Logger.getLogger("notRootLogger);

^当我不想要的东西去控制台.

-要么-

static Logger log = Logger.getRootLogger();

当我做的

在log4.properties中必须做什么才能阻止写入toRootLogger的东西以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相关文章