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

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

问题描述

我在我的应用程序中使用 log4j2.

I'm using log4j2 in my application.

我想要的是调试"之前的所有内容都可以进入控制台,信息"之前的所有内容都可以进入 myapp.log,并且只有信息"可以进入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 主要包括对数据的成功修改(例如用户创建"、用户更新"、用户删除"等).如果是有效的数据修改审核日志.

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.

但我不知道该怎么做.

如何仅获取信息"以登录到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天全站免登陆