Sunday, September 06, 2009

Log4j - Multiple Loggers

Of course you can have multiple loggers. The difference is to add the logger at the beginning. See below and watch for the comma!

# Setting up the different loggers.
log4j.rootLogger=,MONITOR, LOGFILE

Then specify the monitor logger:

# Appender setup A1 = MONITOR
log4j.appender.MONITOR=org.apache.log4j.ConsoleAppender
log4j.appender.MONITOR.Threshold=INFO

And the FileLogger:

# Appender setup A2 = LOGFILE
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOGFILE.Threshold=DEBUG
log4j.appender.LOGFILE.MaxFileSize=10MB
log4j.appender.LOGFILE.File=/usr/home/props/logs/MonFilLogger.log

Specify for both the pattern:

# MONITOR Uses Pattern Layout
log4j.appender.MONITOR.layout=org.apache.log4j.PatternLayout
log4j.appender.MONITOR.layout.ConversionPattern=%m%n

# LOGFILE uses Pattern Layout
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.additivity.LOGFILE=true

This works for me. Perfect.

No comments: