简体   繁体   English

如何用log4j2只记录一个级别?

[英]how to log only one level with log4j2?

I'm using log4j2 in my application. 我在应用程序中使用log4j2。

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'. 我想要的是将要进行调试的所有内容都放入控制台,将要进行“信息”的所有内容都进入myapp.log,仅将“信息”的所有内容都进入“ myapp-audit.log”。

The reason is, INFO mostly consists of successful modifications to data (ex. 'user created', 'user updated', 'user deleted', and so on). 原因是,INFO主要包含成功的数据修改(例如,“用户创建”,“用户更新”,“用户删除”等)。 If is effectively an audit log of data modifications. 如果有效,则为数据修改的审核日志。

But I can't get figure out how to do it. 但是我不知道该怎么做。

How do I get ONLY 'info' to get logged to 'myapp-audit.log'? 如何仅获取“信息”以登录到“ 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>

If you specify INFO in the appender-ref, the appender will receive INFO, WARN, ERROR and FATAL events. 如果在appender-ref中指定INFO,则附加程序将接收INFO,WARN,ERROR和FATAL事件。 You can further restrict to only INFO by filtering out WARN, ERROR and FATAL level events: 您可以通过过滤掉WARN,ERROR和FATAL级别的事件来进一步限制为仅INFO:

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

You can't do that directly since the info level includes everything "above" - warn, error, fatal. 您无法直接执行此操作,因为info级别包含“上方”的所有内容-警告,错误,致命。 What you can do is create a separate audit logger. 您可以做的是创建一个单独的审核记录器。

in the class: 在课堂里:

Logger log = LogManager.getLogger("audit");

in the xml: 在xml中:

<Logger name="audit" level="info">
   <Appender-ref ref="AuditFile" level="info" />
</Logger>

Or you can use the RoutingAppender (you can use something other than ThreadContext ): 或者,您可以使用RoutingAppender (可以使用ThreadContext以外的其他东西):

ThreadContext.put("ROUTINGFLAG", "audit");
log.info("...");
ThreadContext.remove("ROUTINGFLAG");

in the xml: 在xml中:

...
    <Routing name="Routing">
        <Routes pattern="$${ctx:ROUTINGFLAG}">
            <Route AppenderRef="LogFile"/>              
            <Route AppenderRef="AuditFile" key="audit"/>                  
        </Routes>
    </Routing>
</Appenders>
<Loggers>
    <Root level="debug">
        <Appender-ref ref="Console" level="debug"/>
        <AppenderRef ref="Routing" level="info"/>
    </Root>
</Loggers>

You can use LevelRangeFilter . 您可以使用LevelRangeFilter It has minLevel and maxLevel properties(and onMatch and onMismatch, ofcourse, too). 它具有minLevelmaxLevel属性(当然还有onMatch和onMismatch)。

For example(in json) we need to print log in console only on info and warn levels: 例如(在json中),我们只需要在信息和警告级别上打印控制台中的日志:

"Appenders": {
  "Console": {
    "PatternLayout": {
      "pattern": "%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n"
    },
    "name": "Console",
    "target": "SYSTEM_OUT",
    "LevelRangeFilter": {
      "minLevel": "warn",
      "maxLevel": "info",
      "onMatch": "ACCEPT",
      "onMismatch": "DENY"
    }
  }
}

And if you want only info level then write "info" to both properties min and max. 如果只希望信息级别,则将“ info”写入属性min和max。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM