记录错误到stderr和debug,信息到stdout使用log4j

时间:2022-07-10 21:50:49

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:

我想使用apache log4j将日志记录添加到正在开发的应用程序中。此时,我想将所有的日志消息重定向到level INFO和lower(跟踪、调试),并将所有其他日志消息从WARN和above(错误、致命)重定向到stderr。例如:

...
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应该是什么。属性文件吗?这里它是如何看待这个时刻的:

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.

上面配置的问题是logger.error()…也在stdout印刷。

5 个解决方案

#1


9  

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.

不幸的是,没有最大阈值,因此无论您在哪里获得调试消息,您都将获得警告消息。我觉得这有点痛苦。

#2


45  

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

下面以属性文件格式解决。关键是要正确定义过滤器。Log4j属性信息在这里

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


5  

  1. instead of using .properties configuration you must use a .xml configuration.
  2. 必须使用.xml配置而不是.properties配置。
  3. In .xml config put an element named "filter" of type "org.apache.log4j.varia.LevelRangeFilter" and set its parameters LevelMax and LevelMin.
  4. 在.xml配置中,放置一个名为“filter”的类型的元素。org.apache.log4j.varia。“水平范围过滤器”,并设置其参数为水平最大值和水平最小值。

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配置定义了两个附件:stdout和stderr。stdout将所有级别小于或等于INFO的日志打印到stdout, stderr将所有级别大于INFO的日志打印到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

有关更多信息,请参见:http://www.liluna.de/articles/posts/log4j -tutorial.html

#4


1  

another example using the ThresholdFilter markup from log4j2.

另一个使用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.

看医生。

#5


0  

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

这是logback配置,它正好(!)

<?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>

#1


9  

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.

不幸的是,没有最大阈值,因此无论您在哪里获得调试消息,您都将获得警告消息。我觉得这有点痛苦。

#2


45  

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

下面以属性文件格式解决。关键是要正确定义过滤器。Log4j属性信息在这里

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


5  

  1. instead of using .properties configuration you must use a .xml configuration.
  2. 必须使用.xml配置而不是.properties配置。
  3. In .xml config put an element named "filter" of type "org.apache.log4j.varia.LevelRangeFilter" and set its parameters LevelMax and LevelMin.
  4. 在.xml配置中,放置一个名为“filter”的类型的元素。org.apache.log4j.varia。“水平范围过滤器”,并设置其参数为水平最大值和水平最小值。

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配置定义了两个附件:stdout和stderr。stdout将所有级别小于或等于INFO的日志打印到stdout, stderr将所有级别大于INFO的日志打印到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

有关更多信息,请参见:http://www.liluna.de/articles/posts/log4j -tutorial.html

#4


1  

another example using the ThresholdFilter markup from log4j2.

另一个使用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.

看医生。

#5


0  

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

这是logback配置,它正好(!)

<?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>