根据Logback.xml中的特定条件创建不同的MarkerFilter

时间:2018-02-16 12:14:53

标签: java log4j logback slf4j

最近我一直在使用logback,一切顺利,直到我尝试将Marker Filters与条件xml标签一起使用。

我想要做的是建立一个标记过滤系统,根据之前设置的属性,为我的appender选择合适的标记过滤器。

我目前的代码如下:

<configuration scan="true" scanPeriod="30 seconds">
    <if condition='property("severityLevel").equals("SEVERITY-2")'>
        <then>
            <turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
                <Marker>SEVERITY-1</Marker>
                <OnMatch>DENY</OnMatch>
           </turboFilter>
        </then>
    </if>

    <!-- Console Appender -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level - ${appName} - %marker - %logger{36} - %msg%n</pattern>
        </encoder>
     </appender>

    <root level="INFO">
        <appender-ref ref="FILE"/>
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

属性severityLevel来自我在初始化记录器之前设置的configurator属性:

LoggerContext aLoggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator aConfigurator = new JoranConfigurator();
aConfigurator.setContext(aLoggerContext);
aLoggerContext.reset();

aLoggerContext.putProperty(APPLICATION_NAME, config.getApplicationName());
aLoggerContext.putProperty(LOG_MAX_FILE_SIZE, config.getLogMaxFileSize().toString());
aLoggerContext.putProperty(LOG_KEEP_FOR_DAYS, config.getLogKeepForDays().toString());
aLoggerContext.putProperty("severityLevel", "SEVERITY-2");

System.out.println(aLoggerContext.getProperty("severityLevel"));

try
{
  aConfigurator.doConfigure(FileUtils.cleanPath(FileUtils.determineServletRealPath(""), false) + LOGBACK_CONFIGURATION_FILE_PATH);
}
catch (JoranException e)
{
  StatusPrinter.printIfErrorsOccured(aLoggerContext);
  throw e;
}

预期的行为是仅记录SEVERITY-2级别的消息。相反,我不断收到所有消息。

如果我从logback.xml文件中取出If条件它将正常工作但我仍然需要为SEVERITY-3提供另一个条件,其中级别2和级别1都将被拒绝,因此我需要如果条件。

日志中的一些状态消息:

 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [vfs:/Applications/wildfly-10.1.0.Final/standalone/deployments/mw-file-extractor.war/WEB-INF/classes/logback.xml]
 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@6b14b260 - URL [vfs:/Applications/wildfly-10.1.0.Final/standalone/deployments/mw-file-extractor.war/WEB-INF/classes/logback.xml] is not of type file
 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [vfs:/Applications/wildfly-10.1.0.Final/standalone/deployments/mw-file-extractor.war/WEB-INF/classes/logback.xml] 
 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 30 seconds
 |-ERROR in ch.qos.logback.core.joran.conditional.IfAction - Could not find Janino library on the class path. Skipping conditional processing.
 |-ERROR in ch.qos.logback.core.joran.conditional.IfAction - See also http://logback.qos.ch/codes.html#ifJanino
 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@726fa157 - Registering current configuration as safe fallback point

有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

在@Ceki和Logback调试消息的帮助下发现我缺少Janino库以供使用条件。

添加janino-3.0.8.jar和commons-compiler-3.0.8.jar解决了这个问题。