【问题标题】:Log4j2 - debug-level logging except for SpringLog4j2 - 除 Spring 外的调试级日志记录
【发布时间】:2014-10-14 16:42:30
【问题描述】:

我希望我的应用程序在调试级别运行,但 Spring 除外,它会产生大量的日志记录语句,这使得阅读日志变得很困难。我目前已经像这样配置了我的 Log4j2 文件:

<Configuration status="debug">
    <Appenders>
        <RollingFile name="systemLog" fileName="C:/test/logs/system.log" includeLocation="true"
                    filePattern=""C:/test/logs/system-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%d{ISO8601} - %-5level [%t] %C %M %m%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="15 MB"/>
            </Policies>
        </RollingFile>
        <Async name="systemAsyncAppender" includeLocation="true">
            <AppenderRef ref="systemLog" />
        </Async>
    </Appenders>
    <Loggers>
        <SpringLogger name="org.springframework.*" level="error" additivity="false">
            <AppenderRef ref="systemAsyncAppender" />
        </SpringLogger>
        <Root level="debug" includeLocation="true">
            <AppenderRef ref="systemAsyncAppender" />
        </Root>
    </Loggers>
</Configuration>

我认为设置 SpringLogger 会将 Spring 设置为仅在 ERROR 级别运行,并且写入与其他所有内容相同的日志,但我仍然看到完整的 Spring DEBUG 输出。我对使用旧的log4j.properties 文件(我认为它就像log4j.category.org.springframework=ERROR 一样简单)很容易做到这一点有模糊的记忆,但我不确定如何使用 Log4J2 做到这一点。

(使用 Log4J2 2.0.2,spring 3.2.11)

【问题讨论】:

  • 我在任何地方都没有看到&lt;SpringLogger&gt; 元素。为什么不声明一个普通的&lt;logger&gt;name="org.springframework"(注意没有*)?
  • @SotiriosDelimanolis:/facepalm 是的……就是这样。出于某种原因,我认为我需要给记录器自己的 element 名称。太棒了! :P 你的建议奏效了,所以如果你把它写成答案,我会接受它。

标签: java spring log4j log4j2


【解决方案1】:

我认为 log4j2 对 Spring 没有任何特殊配置,所以 &lt;SpringLogger&gt; 在这里似乎不合适。

相反,只需声明一个常规记录器

<logger name="org.springframework" level="error" additivity="false">
    ...
</logger>

还要注意记录器的名称。这些是分层的。你不需要*(我认为它实际上破坏了它)。

【讨论】:

    猜你喜欢
    • 2021-11-19
    • 2014-02-28
    • 1970-01-01
    • 2017-12-20
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    相关资源
    最近更新 更多