Loading...

Log output of a specific Class to a specific Appender with log4j

An Java application uses log4j. Since I use logback all the time, it was quite a challenge to look up the docs for log4j 1.2.17. This application also writes statistical logs. To separate the statistic, we can use a different appender in log4j. The crucial point is to set the additivity to false, so the log won’t be logged twice in the root logger.

# write statistic to separate log file
log4j.appender.STAT=org.apache.log4j.RollingFileAppender
# define file, where information is stored
log4j.appender.STAT.File=/var/log/RiskShield/stats.log
# define layout
log4j.appender.STAT.layout=org.apache.log4j.PatternLayout
log4j.appender.STAT.layout.ConversionPattern=%m%n
log4j.logger.com.riskshield.server.plugin.StatisticPlugin=INFO, STAT
# don't use root logger
log4j.additivity.com.riskshield.server.plugin.StatisticPlugin=false