log4j中logger标签中additivity属性的用法说明

log4j中logger标签中additivity属性的用法说明

log4j logger标签中additivity属性

将logger中的 additivity 属性配置为 false,则这个logger不会将日志流反馈到root中。

可以达到以logger中配置的appender方式来输出日志而其他地方输出的目的,看配置:

< appender  name = "DEMO"  class = "com.XXXXX.RollingFileAppender" >
     < param  name = "file"  value = "${loggingRoot}/xxx.log"  />
     < param  name = "append"  value = "true"  />
     < param  name = "encoding"  value = "GB2312"  />
     < param  name = "threshold"  value = "info"  />
     < param  name = "MaxFileSize"  value = "50MB"  />
     < param  name = "MaxBackupIndex"  value = "10"  />
     < layout  class = "org.apache.log4j.PatternLayout" >
         < param  name = "ConversionPattern"  value = "%d [%X{requestURIWithQueryString}] %-5p %c{2} - %m%n"  />
     </ layout >
</ appender >
< logger  name = "XXXX.XXXX.XXXX"  additivity = "false" >
     < level  value = "${loggingLevel}"  />
     < appender-ref  ref = "DEMO"  />
</ logger >

root的作用是收集下面所有反馈上来的信息流并根据配置在root中appender进行输出,只要你在looger中配置了additivity="false",就不会反馈到root中。

意味着不会重复输出!

log4j.additivity用法和例子

因为需要将packageOne和packageTwo的日志写入各自单独的文件,同时在总的日志中不出现packageOne和PackageTwo的日志。

可以使用log4j.additivity。因为log4j是层次的,如果没有log4j.additivity默认写入到packageOneFileAppender和packageTwoFileAppender的都会写入rootLogger的rootFileAppender

log4j.logger.com.demo.packageOone = INFO, packageOneFileAppender
log4j.additivity.com.demo.

packageOne

= false
log4j.category.com.demo.packageTwo = INFO, packageTwoFileAppender
log4j.additivity.com.demo.packageTwo = false
log4j.rootLogger = INFO, rootFileAppender

以上为个人经验,希望能给大家一个参考,也希望大家多多支持云海天教程。