Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j different appenders for different log levels using properties file

Since the official documentation for log4j is a broken link, and the getting started guide isn't helping me, I would like to know how to use two appenders with different log levels for the same logger, using a .properties file instead of an .xml one.

For example, like this:

log4j.rootLogger=debug,  APPENDER_FILE, APPENDER_STDOUT 

I'm logging with two different appenders, but on the same log level (DEBUG). I would like to use an INFO log level for APPENDER_STDOUT, and aDEBUG log level for APPENDER_FILE.

My question is similar to this, but unlike that question, I'm using the log4j.properties file, not the xml file.

like image 345
alessiop86 Avatar asked Sep 27 '12 10:09

alessiop86


People also ask

How do I change log level in log4j?

Setting Levels using Configuration File log4j provides you configuration file based level setting which sets you free from changing the source code when you want to change the debugging level. Following is an example configuration file which would perform the same task as we did using the log. setLevel(Level.


2 Answers

You need to set the logger to the more detailed of the two levels (DEBUG) but then set a threshold on the appender to tell it to only show messages at INFO and above

log4j.appender.APPENDER_STDOUT.Threshold=INFO 
like image 116
Ian Roberts Avatar answered Sep 20 '22 00:09

Ian Roberts


Also, if you want to specify the appender at the package level, you can use:

log4j.logger.[package]=[Level], [Appender] 

Example:

log4j.logger.org.apache.cxf=INFO, MyAppender 

For more details, please, take a look at: https://stackoverflow.com/a/19795886/679240

like image 24
Haroldo_OK Avatar answered Sep 19 '22 00:09

Haroldo_OK