简体   繁体   English

slf4j配置以记录单个文件

[英]slf4j configuration to log a single file

logback.spring.xml is configured to log only on single class logback.spring.xml配置为仅登录单个类

<logger name="classname">
        <appender-ref ref="AUDIT_LOG"/>
</logger>

In the class there is only 1 call to log. 在课堂上,只有1个通话记录。 But when i look into the log file created, there is 40 000 + lines of nonsense. 但是,当我查看创建的日志文件时,有40000多行废话。 There is the 1 line i wanted, but the other 40 000 lines should not be there. 有我想要的1行,但其他40 000行不应该在那里。

How do i have to configure the logback to make sure that the log file will only contain the 1 log invcation and nothing else? 我如何配置回送以确保该日志文件仅包含1个日志邀请,而没有其他内容?

 <appender name="AUDIT_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_PATH}/audit/audit.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_PATH}/audit/audit.%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>10</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>10MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <charset>utf-8</charset>
            <Pattern>${FILE_LOG_PATTERN}</Pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
</appender>

It sounds like you want the AUDIT_LOG to be reserved for one logger and only for that logger. 听起来您想要为一个记录器保留AUDIT_LOG ,而仅为该记录器保留。

This instruction ... 本指令...

<logger name="classname">
    <appender-ref ref="AUDIT_LOG"/>
</logger>

... directs log events from the classname logger to AUDIT_LOG but it does not prevent other log events from being handled by that appender. ...将日志事件从classname记录器定向到AUDIT_LOG但它不会阻止该附加程序处理其他日志事件。

If you want to ensure that the AUDIT_LOG appender only handles events for a specific logger then you can use a EvaluatorFilter . 如果要确保AUDIT_LOG附加程序仅处理特定记录器的事件,则可以使用EvaluatorFilter

Here's an example using the Logback's JaninoEventEvaluator : 这是使用Logback的JaninoEventEvaluatorJaninoEventEvaluator

<appender name="AUDIT_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!-- this filter will accept all log events having the logger name "classname" -->
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
        <evaluator>
            <expression>return logger.equals("classname");</expression>
        </evaluator>
        <OnMismatch>DENY</OnMismatch>
        <OnMatch>NEUTRAL</OnMatch>
    </filter>

    <file>${LOG_PATH}/audit/audit.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>${LOG_PATH}/audit/audit.%i.log.gz</fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>10</maxIndex>
    </rollingPolicy>
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxFileSize>10MB</maxFileSize>
    </triggeringPolicy>
    <encoder>
        <charset>utf-8</charset>
        <Pattern>${FILE_LOG_PATTERN}</Pattern>
    </encoder>
</appender>

Note: the evaluator expression is an ... 注意:求值器表达式是一个...

arbitrary Java language block returning a boolean value as the evaluation criteria 返回布尔值作为评估标准的任意Java语言块

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

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