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

以下是我正在使用的logback.xml文件以及出现的错误。登录网站上的示例显示了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

最佳答案

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

关于java - 在FileAppender中使用Logback Marker时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14209701/

10-10 23:23