如何避免在log4j v.1中向根记录器写入消息[英] How to avoid writing messages to the root logger in log4j v.1

本文是小编为大家收集整理的关于如何避免在log4j v.1中向根记录器写入消息的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

是否有一些路径仅将日志消息写入"子"记录器,避免根记录器?根记录器正在被其他组件使用,因此无法降低其级别或禁用附加程序.谢谢

推荐答案

请使用 Log4j 可加性.

将 Log4j 记录器的可加性属性设置为 false,然后到达该记录器的日志消息将不会传播到父记录器.

Log4j 配置文件:

log4j.category.com.demo.moduleone = INFO, moduleOneFileAppender
log4j.additivity.com.demo.moduleone = false
log4j.category.com.demo.moduletwo = INFO, moduleTwoFileAppender
log4j.additivity.com.demo.moduletwo = false
log4j.rootLogger = INFO, rootFileAppender

通过上述配置,来自 com.demo.moduleone 的日志消息将仅发送到 moduleOneAppender,其余日志消息将发送到 rootFileAppender.

本文地址:https://www.itbaoku.cn/post/1575096.html

问题描述

Is some path to write log messages only to the 'child' logger, avoiding root logger? The root logger is using by other components, so there is no ability to decrease it's level or disable appender. thanks

推荐答案

Please use Log4j additivity.

Set the additivity property of a Log4j logger to false and then the log messages which are coming to that logger will not be propagated to parent loggers.

Log4j configuration file:

log4j.category.com.demo.moduleone = INFO, moduleOneFileAppender
log4j.additivity.com.demo.moduleone = false
log4j.category.com.demo.moduletwo = INFO, moduleTwoFileAppender
log4j.additivity.com.demo.moduletwo = false
log4j.rootLogger = INFO, rootFileAppender

With the above configuration, the log messages from the com.demo.moduleone will go to the moduleOneAppender only and the rest of the log messages will go to the rootFileAppender.