Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging error to stderr and debug, info to stdout with log4j

I want to add logging to an application I am developing, using apache log4j. At this point I want to redirect all log messages for level INFO and lower (TRACE, DEBUG) to stdout and all other log messages from WARN and above (ERROR, FATAL) to stderr. For example:

... logger.info("Processing at some point"); // must be written to stdout logger.debug("Point x was processed"); // must be written to stdout logger.warn("Incorrect point config"); // must be written only to stderr logger.error("Exception occurred at point x"); // must be written only to stderr 

So what should be my log4j.properties file? Here how it looks at this momment:

log4j.rootLogger=DEBUG, stdout, stderr  # configure stdout # set the conversion pattern of stdout # Print the date in ISO 8601 format log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Threshold = DEBUG log4j.appender.stdout.Target   = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %-5p %d [%t][%F:%L] : %m%n  # configure stderr # set the conversion pattern of stdout # Print the date in ISO 8601 format log4j.appender.stderr = org.apache.log4j.ConsoleAppender log4j.appender.stderr.Threshold = WARN log4j.appender.stderr.Target   = System.err log4j.appender.stderr.layout = org.apache.log4j.PatternLayout log4j.appender.stderr.layout.ConversionPattern = %-5p %d [%t][%F:%L] : %m%n 

The problem with the above configuration is that the logger.error() ... is printed at stdout too.

like image 962
Elvis Avatar asked Dec 13 '11 12:12

Elvis


People also ask

What is debug in log4j?

Debug is used a lot for debugging the application at development time. Every log message will appear to log files once this level is set. It basically belongs to developers. INFO. The INFO logging level is used to record messages about routine application operation.

Where is log4j properties file located?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file.


1 Answers

Solved below also in properties file format. The trick was to get the filter definition right. Log4j properties info here

The original attempt, modified to filter WARN & ERROR:

log4j.rootLogger=TRACE, stdout, stderr  # configure stdout # set the conversion pattern of stdout # Print the date in ISO 8601 format log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Threshold = TRACE log4j.appender.stdout.Target   = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %-5p %d [%t][%F:%L] : %m%n log4j.appender.stdout.filter.filter1=org.apache.log4j.varia.LevelRangeFilter log4j.appender.stdout.filter.filter1.levelMin=TRACE log4j.appender.stdout.filter.filter1.levelMax=INFO  # configure stderr # set the conversion pattern of stdout # Print the date in ISO 8601 format log4j.appender.stderr = org.apache.log4j.ConsoleAppender log4j.appender.stderr.Threshold = WARN log4j.appender.stderr.Target   = System.err log4j.appender.stderr.layout = org.apache.log4j.PatternLayout log4j.appender.stderr.layout.ConversionPattern = %-5p %d [%t][%F:%L] : %m%n 
like image 69
Jonas Berlin Avatar answered Sep 23 '22 08:09

Jonas Berlin