Home » Java » Logging error to stderr and debug, info to stdout with log4j

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

Posted by: admin December 20, 2017 Leave a comment

Questions:

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.

Answers:

Per Jon Skeet’s previous post at Post

unfortunately there is no maximum threshold, so wherever you get
debug messages, you also get warning messages. That’s a bit of a pain,
IMO.

Questions:
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

Questions:
Answers:
  1. instead of using .properties configuration you must use a .xml configuration.
  2. In .xml config put an element named “filter” of type “org.apache.log4j.varia.LevelRangeFilter” and set its parameters LevelMax and LevelMin.

For example the following .xml configuration define two appenders: stdout, stderr. The stdout prints all logs that have a level less than or equal to INFO to stdout, and the stderr prints all logs with level greater than INFO to stderr.

<?xml version="1.0" encoding="UTF-8"?>
<!--
    Document    : log4j.xml
    Created on  : 13 Δεκέμβριος 2011, 3:55 μμ
    Author      : elitex
    Description : Purpose of the document follows.
-->
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
    <appender name="stderr" class="org.apache.log4j.ConsoleAppender">
        <param name="threshold" value="warn" />
        <param name="target" value="System.err"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d [%t][%F:%L] : %m%n" />
        </layout>
    </appender>
    <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
        <param name="threshold" value="debug" />
        <param name="target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d [%t][%F:%L] : %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="debug" />
            <param name="LevelMax" value="info" />
        </filter>
    </appender>
    <root>
        <priority value="debug"></priority>
        <appender-ref ref="stderr" />
        <appender-ref ref="stdout" />
    </root>
</log4j:configuration>

For more info look at: http://www.laliluna.de/articles/posts/log4j-tutorial.html

Questions:
Answers:

another example using the ThresholdFilter markup from log4j2.

<Console name="ConsoleERR" target="SYSTEM_ERR">
    <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level - %msg%n"/>
    <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
</Console>

See the doc.