场景要求:原工程中,添加了控制台(Console)及文件输出(DailyRollingFile)。现需要将接口部分单独一个日志文件以方便监控。
实现:如下示例代码,结果DailyRollingFile的内容也将包含test.interfaces包下面WARN级别的日志输出。
<logger name="test.interfaces" > <level value="INFO"/> <appender-ref ref="demoAppender"/> </logger> <root> <level value="WARN"/> <appender-ref ref="CONSOLE"/> <appender-ref ref="DailyRollingFile"/> </root>
如果test.interfaces包下需要单独输出日志文件,且总的日志文件并不显示。则需要使用additivity属性。默认为true,
标记日志记录器(Logger)的关联性。
<logger name="test" additivity="false"> <level value="DEBUG"/> <appender-ref ref="DEMO"/> </logger>
摘录log4j官方说明:
Additivity
The output of a log statement of logger C will go to all the appenders in C and its ancestors. This is the meaning of the term "appender additivity".
However, if an ancestor of logger C, say P, has the additivity flag set to false, then C's output will be directed to all the appenders in C and it's ancestors upto and including P but not the appenders in any of the ancestors of P.
Loggers have their additivity flag set to true by default.