log4j配置文件中的additivity属性

时间:2022-03-17 13:47:36
它是 子Logger 是否继承 父Logger 的 输出源(appender)的标志位。具体说,默认情况下子Logger会继承父Logger的appender,也就是说子Logger会在父Logger的appender里输出。若是additivity设为false,则子Logger只会在自己的appender里输出,而不会在父Logger的appender里输出。

使用例子:下面是一个log4j的配置文件,父logger是timer和error,子logger是net

#
# Priority = {FATAL|ERROR|WARN|INFO|DEBUG}
# # +---------------------------------------------------+
# | root |
# +---------------------------------------------------+ # Category - Root
# ================
log4j.rootLogger=info,timer,error log4j.logger.timer=info,timer
log4j.additivity.timer=false log4j.logger.net=info,net
log4j.additivity.net=false log4j.logger.error=error,error
log4j.additivity.error=false # Category - hibernate
# ========================================
log4j.category.org.springframework=ERROR
log4j.logger.com.mchange.v2=ERROR
# +---------------------------------------------------+
# | Appender |
# +---------------------------------------------------+
#
# %c Fully class name. 输出所属的类目,通常就是所在类的全名;
# %d Default is ISO8601. Custom:%d{yyy-MM-dd HH:mm:ss }, output example:2002-10-18 22:10:28
# %f Class name.
# %l Location and line number 输出日志事件的发生位置,及在代码中的行数;
# %m Message 输出代码中指定的消息;
# %n Nextline 输出一个回车换行符,Windows平台为"rn”,Unix平台为"n”;
# %p Log priority 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL;
# %r Log consumed time 输出自应用启动到输出该log信息耗费的毫秒数;
# %t Thread name 输出产生该日志事件的线程名;
# # Appender - stdout
# =========================================
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %l %p: %m%n
log4j.appender.stdout.encoding=UTF-8 log4j.appender.timer=org.apache.log4j.DailyRollingFileAppender
log4j.appender.timer.File=/data/timer_test/log/timer.log
log4j.appender.timer.layout=org.apache.log4j.PatternLayout
log4j.appender.timer.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %l %p: %m%n
log4j.appender.timer.encoding=UTF-8 log4j.appender.net=org.apache.log4j.DailyRollingFileAppender
log4j.appender.net.File=/data/timer_test/log/net.log
log4j.appender.net.layout=org.apache.log4j.PatternLayout
log4j.appender.net.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %l %p: %m%n
log4j.appender.net.encoding=UTF-8 log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.Threshold=ERROR
log4j.appender.error.File=/data/timer_test/log/error.log
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %l %p: %m%n
log4j.appender.error.encoding=UTF-8