How can I get log4j to write out to multiple logs?

Byron Tymvios

You need to set up more categories in the log4j.properties file, this can be done like so:

# The default root appender
log4j.rootLogger=A1

# A1 is set to be a ConsoleAppender which outputs to System.out.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-22d{dd/MMM/yyyy HH:mm:ss} %-8p %c [%t] - %m (%l)%n

# An extra category to a log file
log4j.category.AppLogging=A2

# A3 is set to be a FileAppender which will log all actions in the application.
log4j.appender.A3=org.apache.log4j.FileAppender
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=%-22d{dd/MMM/yyyy HH:mm:ss} - %m%n
log4j.appender.A3.file=application.log

Now in your code if you want a Logger for the root appender you use:

Logger logger = Logger.getLogger(MyClass.class.getName());

And if you want to log to the alternate category:

Logger appLogger = Logger.getLogger("AppLogging");

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact