Zurück zur Übersicht

Florian Hopf

24.11.2010

Logging to multiple files using log4j

Sometimes even the simple things take quite some time. For a special application log I’ve been searching for a way to write log calls for one package to a separate file using Log4j. I’ve been searching the web, skimming through the log4j documentation but didn’t really find anything. This message, though only a question, contained the missing hint.

Suppose you have a log configuration that already has root loggers configured. To make only one package log to another file, but not to the root logger appenders, you add something like this:

`log4j.logger.my.Logger=INFO,FILE

log4j.additivity.my.Logger=false

log4j.appender.FILE=org.apache.log4j.RollingFileAppender

log4j.appender.FILE.File=include-performance.log

log4j.appender.FILE.layout=org.apache.log4j.PatternLayout

log4j.appender.FILE.layout.ConversionPattern=%d{DATE} %5p %30.30C:%4L %m%n`

What does it do? In the first line you create a logger that logs anything above info inclusive. In the same line, separated by a comma, you add the name of your appender. By setting additivity to false you prevent the logger to inherit any loggers from ancestors (including any root loggers).