Nacht
Nacht

Reputation: 11346

Logging based on levels on log4j

I'm trying to log everything that happens in my application to two logs: one log will have everything, the other will only have INFO and upwards (so, one file will have everything, while the other will only have logs of level INFO, WARN, ERROR and so on). The first file is meant for the devs, while the second for the testers.

Anyone can point me in the right direction here? I read this article about custom filters:

http://veerasundar.com/blog/2011/05/log4j-tutorial-writing-different-log-levels-in-different-log-files/

but I was thinking that since all I want is exclude one level from one file, that there might be an easier, native way of doing this. Can someone give me an example of how I would do this in the properties file?

Thanks.

Upvotes: 1

Views: 2546

Answers (1)

John B
John B

Reputation: 32949

Point you logger to two appenders. One with a threshold set to Info.

Good Example

Copied from example...

 log4j.rootLogger=DEBUG, CA, FA

 # Console Appender
 log4j.appender.CA=org.apache.log4j.ConsoleAppender
 log4j.appender.CA.layout=org.apache.log4j.PatternLayout
 log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
    
 # File Appender
 log4j.appender.FA=org.apache.log4j.FileAppender
 log4j.appender.FA.File=sample.log
 log4j.appender.FA.layout=org.apache.log4j.PatternLayout
 log4j.appender.FA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

 # Set the logger level of File Appender to WARN
 log4j.appender.FA.Threshold = WARN

Upvotes: 4

Related Questions