如何用log4j2只记录一个级别? [英] how to log only one level with log4j2?

查看:102
本文介绍了如何用log4j2只记录一个级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了log4j2。

I'm using log4j2 in my application.

我想要的是调试到控制台的所有内容,一切都要去'信息'去到myapp.log,只有'info'去'myapp-audit.log'。

What I want is everything up to 'debug' to go to console, everything up to 'info' to go to myapp.log, and ONLY 'info' to go to 'myapp-audit.log'.

原因是,INFO主要包括对数据的成功修改(ex 。'用户创建','用户更新','用户删除'等等)。如果实际上是数据修改的审计日志。

The reason is, INFO mostly consists of successful modifications to data (ex. 'user created', 'user updated', 'user deleted', and so on). If is effectively an audit log of data modifications.

但我无法弄明白该怎么做。

But I can't get figure out how to do it.

我如何才能获得'info'以登录'myapp-audit.log'?这是我目前的配置......

How do I get ONLY 'info' to get logged to 'myapp-audit.log'? Here's my current configuration ...

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>

        <File name="LogFile" fileName="myapp.log">
            <PatternLayout
                pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </File>

        <File name="AuditFile" fileName="myapp-audit.log">
            <PatternLayout
                pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </File>
    </appenders>

    <loggers>
        <root level="debug">
            <appender-ref ref="Console" level="debug" />
            <appender-ref ref="LogFile" level="info" />
            <appender-ref ref="AuditFile" level="info" /> <!-- I want ONLY 'info' here -->
        </root>
    </loggers>
</configuration>


推荐答案

如果在appender-ref中指定INFO, appender将收到INFO,WARN,ERROR和FATAL事件。您可以通过过滤掉WARN,ERROR和FATAL级别事件来进一步限制为仅INFO:

If you specify INFO in the appender-ref, the appender will receive INFO, WARN, ERROR and FATAL events. You can further restrict to only INFO by filtering out WARN, ERROR and FATAL level events:

<File name="AuditFile" fileName="myapp-audit.log">
    <PatternLayout 
       pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%nZ" />
    <Filters>

        <!-- First deny warn, error and fatal messages -->
        <ThresholdFilter level="warn"  onMatch="DENY" onMismatch="NEUTRAL"/>
        <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
        <ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>

        <!-- Then accept info, warn, error, fatal and deny debug/trace -->
        <ThresholdFilter level="info"  onMatch="ACCEPT" onMismatch="DENY"/>
    </Filters>
</File>

这篇关于如何用log4j2只记录一个级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆