在FileAppender中使用Logback Marker时出错

时间:2013-01-08 06:54:23

标签: java logback marker fileappender

我已经开始为我们的应用程序探索Logback选项。其中一个要求是为具有特定“标记”的日志条目创建单独的日志文件。

下面是我正在使用的logback.xml文件以及我收到的错误。 logback网站上的示例显示了SMTPAppender的用法,但我想使用FileAppender。这可能吗?如果没有,我还有其他选择吗?

<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 个答案:

答案 0 :(得分:17)

在SMTPAppender中,评估程序用于触发。在FileAppender中,您需要将评估程序封装在筛选器中,准确地evaluator filter。这是一个例子:

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