Logging error to stderr and debug, info to stdout

2019-01-21 01:30发布

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.

6条回答
霸刀☆藐视天下
2楼-- · 2019-01-21 01:44

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
查看更多
孤傲高冷的网名
3楼-- · 2019-01-21 01:46

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.

查看更多
混吃等死
4楼-- · 2019-01-21 01:55
  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

查看更多
成全新的幸福
5楼-- · 2019-01-21 01:57

This is logback config which does exactly(!) what you asking for:

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="30 seconds">

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <target>System.out</target>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <encoder>
            <pattern>
                %-4relative [%thread] %-5level %logger - %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
        <target>System.err</target>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>WARN</level>
        </filter>
        <encoder>
            <pattern>
                %-4relative [%thread] %-5level %logger - %msg%n
            </pattern>
        </encoder>
    </appender>

    <root level="${LOGBACK_ROOT_LEVEL:-INFO}">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="STDERR" />
    </root>

</configuration>
查看更多
女痞
6楼-- · 2019-01-21 01:59

Changing it to NEUTRAL and adding filter for DEBUG should let it capture DEBUG as well. There is another built-in filter which requires janino dependency:

    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">      
        <evaluator> 
            <expression>
                   <![CDATA[ level > INFO ]]>
            </expression>
        </evaluator>
        <OnMatch>DENY</OnMatch>
        <OnMismatch>NEUTRAL</OnMismatch>
    </filter>
查看更多
别忘想泡老子
7楼-- · 2019-01-21 02:00

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.

查看更多
登录 后发表回答