根据Logback.xml中的某个条件创建不同的MarkerFilters

Creating different MarkerFilters according to a certain condition in Logback.xml

最近我一直在使用 logback,一切都很顺利,直到我尝试将标记过滤器与条件 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>

属性 严重级别来自我在初始化记录器之前设置的配置器属性:

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 级都将被拒绝,因此我需要 If 条件。

日志中的一些状态消息:

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

有谁知道我做错了什么吗?

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

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