使用log4j将错误记录到stderr并将调试、信息记录到stdout

amrnrhlw  于 2022-11-06  发布在  其他
关注(0)|答案(6)|浏览(279)

我想使用apache log4j向我正在开发的应用程序添加日志记录。此时,我想将INFO级别及更低级别(TRACE、DEBUG)的所有日志消息重定向到stdout,将WARN级别及更高级别(ERROR、FATAL)的所有其他日志消息重定向到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

那么我的www.example.com文件应该是什么log4j.properties呢?下面是它在这个时刻的样子:

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

上述配置的问题是logger.error()...也在stdout中打印。

cgyqldqp

cgyqldqp1#

下面也以属性文件格式解决了这个问题。诀窍是正确定义过滤器。这里是Log4j属性信息
原始尝试,已修改以筛选警告& 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
piv4azn7

piv4azn72#

根据Jon Skeet在Post上的前一篇文章
不幸的是,没有最大阈值,因此无论您在哪里得到调试消息,您也会得到警告消息。

e5nszbig

e5nszbig3#

1.您必须使用.xml配置,而不是使用.properties配置。
1.在.xml配置中放置一个名为“filter”的元素,其类型为“org.apache.log4j.varia.LevelRangeFilter”,并设置其参数LevelMaxLevelMin
例如,以下.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>

有关更多信息,请访问:http://www.laliluna.de/articles/posts/log4j-tutorial.html

pinkon5k

pinkon5k4#

另一个示例使用来自log4j2的ThresholdFilter标记。

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

请参阅doc

bis0qfac

bis0qfac5#

这是一个完全(!)符合您要求的回登录配置:

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

cgfeq70w6#

将其更改为NEUTRAL并添加DEBUG过滤器应该也可以捕获DEBUG。还有另一个内置过滤器需要janino依赖项:

<filter class="ch.qos.logback.core.filter.EvaluatorFilter">      
        <evaluator> 
            <expression>
                   <![CDATA[ level > INFO ]]>
            </expression>
        </evaluator>
        <OnMatch>DENY</OnMatch>
        <OnMismatch>NEUTRAL</OnMismatch>
    </filter>

相关问题