Error using Logback Marker in FileAppender

2019-04-30 03:03发布

问题:

I have started exploring Logback option for our application. One of the requirement is to create a separate log file for log entries that have a specific "Marker".

Below is the logback.xml file that i am using and the error I am getting. The examples on the logback website shows the usage of SMTPAppender but I would like to use FileAppender instead. Is this possible? If not, what other option do i have?

<property name="USER_HOME" value="c:/temp" />

<appender name="AUDIT_FILE" class="ch.qos.logback.core.FileAppender">        
    <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
        <marker>APPEND_SYSLOG</marker>
    </evaluator>
    <file>${USER_HOME}/mw_syslog.log</file>        
    <encoder>
        <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
    </encoder>
</appender>

<root level="debug">
    <appender-ref ref="AUDIT_FILE" />
</root>


    12:07:01,515 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/C:/JavaProjects/LogbackWeb/target/LogbackWeb-1.0-SNAPSHOT/WEB-INF/classes/logback.xml]
    12:07:02,013 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
    12:07:02,134 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.FileAppender]
    12:07:02,176 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [AUDIT_FILE]
    12:07:02,286 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@43:76 - no applicable action for [evaluator], current pattern is [[configuration][appender][evaluator]]
    12:07:02,286 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@44:21 - no applicable action for [marker], current pattern is [[configuration][appender][evaluator][marker]]
    12:07:02,310 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
    12:07:02,828 |-INFO in ch.qos.logback.core.FileAppender[AUDIT_FILE] - File property is set to [c:/temp/mw_syslog.log]
    12:07:02,836 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
    12:07:02,836 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [AUDIT_FILE] to Logger[ROOT]
    12:07:02,842 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
    12:07:02,855 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@3ad1a015 - Registering current configuration as safe fallback point

回答1:

In SMTPAppender the evaluator is used for triggering. In FileAppender, you need to encapsulate the evaluator within a filter, an evaluator filter to be precise. Here is an example:

<property name="USER_HOME" value="c:/temp" />

<appender name="AUDIT_FILE" class="ch.qos.logback.core.FileAppender">        
    <!-- the filter element -->
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">   
      <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
        <marker>APPEND_SYSLOG</marker>
      </evaluator>
     <onMismatch>DENY</onMismatch>
     <onMatch>NEUTRAL</onMatch>
   </filter>
    <file>${USER_HOME}/mw_syslog.log</file>        
    <encoder>
        <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
    </encoder>
</appender>

<root level="debug">
    <appender-ref ref="AUDIT_FILE" />
</root>