Logback JaninoEventEvaluator将日志事件定向到特定的appender

时间:2017-12-06 16:02:56

标签: java logging logback slf4j

我有一个logback-test xml如下。我想从控制台和GENERIC_LOG_FILE过滤掉包org.jdbcds中的任何日志,并将它们放在appender SQLFILE下的单独文件中。但它们都不起作用。来自org.jdbcdslog的所有日志都记录在控制台和GENERIC_LOG_FILE中,但不在appender SQLFILE中给出的文件下。请帮忙。

  <configuration scan="true">
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
           <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
          <evaluator>
              <expression>return logger.startsWith("org.jdbcdslog")</expression>
           </evaluator>
          <OnMatch>DENY</OnMatch>
          <OnMismatch>NEUTRAL</OnMismatch>
        </filter>
    </appender>
    <appender name="GENERAL_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
          <append>true</append>
           <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
           </encoder>
            <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
                <evaluator>
                    <expression>return logger.startsWith("org.jdbcdslog")</expression>
                </evaluator>
                <OnMatch>DENY</OnMatch>
                <OnMismatch>NEUTRAL</OnMismatch>
            </filter>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${user.home}/server.abacus.%d{yyyy-MM-dd}.log</fileNamePattern>
                <maxHistory>14</maxHistory>
            </rollingPolicy>
   </appender>
   <appender name="SQLFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <!--<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator>
                <expression>message.contains("TestFinished()")</expression>
            </evaluator>
            <OnMatch>DENY</OnMatch>
            <OnMismatch>NEUTRAL</OnMismatch>
        </filter>-->
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator> 
                <expression>return logger.startsWith("org.jdbcdslog");</expression>
            </evaluator>
            <OnMatch>ACCEPT</OnMatch>
            <OnMismatch>DENY</OnMismatch>
        </filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${user.home}/all_query.abacus.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>14</maxHistory>
        </rollingPolicy>
    </appender>
    <logger name="org.jdbcdslog.SlowQueryLogger" level="INFO"/>
    <logger name="org.jdbcdslog.StatementLogger" level="INFO"/>
    <root level="INFO">
      <appender-ref ref="CONSOLE" />
      <appender-ref ref="GENERAL_LOG_FILE" />
      <appender-ref ref="ERROR_LOG_FILE" />
      <appender-ref ref="PERF_LOG_FILE" />
      <appender-ref ref="SQLFILE" />
      <appender-ref ref="SLOWQUERY_SQLFILE" />
   </root>

更新1:

这是修改后的XML

<configuration scan="true">
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
        </encoder>
    </appender>
    <appender name="GENERAL_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator>
                <expression>message.contains("TestFinished()")</expression>
            </evaluator>
            <OnMatch>DENY</OnMatch>
            <OnMismatch>NEUTRAL</OnMismatch>
        </filter>
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator>
                <expression>return logger.startsWith("Perf");</expression>
            </evaluator>
            <OnMatch>DENY</OnMatch>
            <OnMismatch>ACCEPT</OnMismatch>
        </filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${user.home}/server.abacus.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>14</maxHistory>
        </rollingPolicy>
    </appender>
    <appender name="ERROR_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator>
                <expression>message.contains("TestFinished()")</expression>
            </evaluator>
            <OnMatch>DENY</OnMatch>
            <OnMismatch>NEUTRAL</OnMismatch>
        </filter>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <append>true</append>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${user.home}/server.abacus.error.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>14</maxHistory>
        </rollingPolicy>
    </appender>
    <appender name="PERF_LOG_FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator>
                <expression>message.contains("TestFinished()")</expression>
            </evaluator>
            <OnMatch>DENY</OnMatch>
            <OnMismatch>NEUTRAL</OnMismatch>
        </filter>
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator> 
                <expression>return logger.startsWith("Perf");</expression>
            </evaluator>
            <OnMatch>ACCEPT</OnMatch>
            <OnMismatch>DENY</OnMismatch>
        </filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${user.home}/perf.abacus.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>14</maxHistory>
        </rollingPolicy>
    </appender>
    <appender name="SQLFILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${user.home}/all_query.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>14</maxHistory>
        </rollingPolicy>
    </appender>
    <appender name="SLOWQUERY_SQLFILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${user.home}/slow_query.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>14</maxHistory>
        </rollingPolicy>
    </appender>
    <logger name="ab.xyz" level="INFO" />
    <logger name="bd.xyz" level="INFO" />
    <logger name="net.sf.hibernate" level="WARN" />
    <logger name="org.springframework" level="WARN" />
    <logger name="jdbc.sqlonly" level="ERROR" />
    <logger name="Perf" level="INFO" />
    <logger name="org.jdbcdslog.SlowQueryLogger" level="INFO" additivity="false">
        <appender-ref ref="SLOWQUERY_SQLFILE" />
    </logger>
    <logger name="org.jdbcdslog.StatementLogger" level="INFO" additivity="false">
        <appender-ref ref="SQLFILE" />
    </logger>
    <logger name="org.jdbcdslog.ConnectionLogger" level="ERROR"/>
    <logger name="org.jdbcdslog.ResultSetLogger" level="ERROR"/>

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="GENERAL_LOG_FILE" />
        <appender-ref ref="ERROR_LOG_FILE" />
        <appender-ref ref="PERF_LOG_FILE" />
    </root>


</configuration>

更新2:

<configuration scan="true">
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
        </encoder>
    </appender>
    <appender name="GENERAL_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator>
                <expression>message.contains("TestFinished()")</expression>
            </evaluator>
            <OnMatch>DENY</OnMatch>
            <OnMismatch>NEUTRAL</OnMismatch>
        </filter>
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator>
                <expression>return logger.startsWith("Perf");</expression>
            </evaluator>
            <OnMatch>DENY</OnMatch>
            <OnMismatch>ACCEPT</OnMismatch>
        </filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${user.home}/server.abacus.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>14</maxHistory>
        </rollingPolicy>
    </appender>
    <appender name="ERROR_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator>
                <expression>message.contains("TestFinished()")</expression>
            </evaluator>
            <OnMatch>DENY</OnMatch>
            <OnMismatch>NEUTRAL</OnMismatch>
        </filter>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <append>true</append>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${user.home}/server.abacus.error.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>14</maxHistory>
        </rollingPolicy>
    </appender>
    <appender name="PERF_LOG_FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator>
                <expression>message.contains("TestFinished()")</expression>
            </evaluator>
            <OnMatch>DENY</OnMatch>
            <OnMismatch>NEUTRAL</OnMismatch>
        </filter>
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator> 
                <expression>return logger.startsWith("Perf");</expression>
            </evaluator>
            <OnMatch>ACCEPT</OnMatch>
            <OnMismatch>DENY</OnMismatch>
        </filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${user.home}/perf.abacus.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>14</maxHistory>
        </rollingPolicy>
    </appender>
    <appender name="SQLFILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${user.home}/all_query.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>14</maxHistory>
        </rollingPolicy>
    </appender>
    <appender name="SLOWQUERY_SQLFILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss}|%level|%logger|%msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${user.home}/slow_query.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>14</maxHistory>
        </rollingPolicy>
    </appender>
    <logger name="ab.xyz" level="INFO" />
    <logger name="cd.xyz" level="INFO" />
    <logger name="net.sf.hibernate" level="WARN" />
    <logger name="org.springframework" level="WARN" />
    <logger name="jdbc.sqlonly" level="ERROR" />
    <logger name="Perf" level="INFO" />
    <logger name="org.jdbcdslog.SlowQueryLogger" level="INFO" additivity="false">
        <appender-ref ref="SLOWQUERY_SQLFILE" />
    </logger>
    <logger name="org.jdbcdslog.StatementLogger" level="INFO" additivity="false">
        <appender-ref ref="SQLFILE" />
    </logger>
    <logger name="org.jdbcdslog.ConnectionLogger" level="ERROR"/>
    <logger name="org.jdbcdslog.ResultSetLogger" level="ERROR"/>

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="GENERAL_LOG_FILE" />
        <appender-ref ref="ERROR_LOG_FILE" />
        <appender-ref ref="PERF_LOG_FILE" />
        <appender-ref ref="SQLFILE" />
        <appender-ref ref="SLOWQUERY_SQLFILE" />
    </root>
</configuration>

1 个答案:

答案 0 :(得分:1)

为了达到这个目的:

  

我想从控制台和GENERIC_LOG_FILE过滤掉包org.jdbcds中的所有日志,并将它们放在appender SQLFILE下的单独文件中

我认为您需要使用Janino,您可以将org.jdbcds记录器与SQLFILE appender相关联,并使用additivity=false来防止此记录器被转发到其他appender。

例如:

<!-- direct logging from org.jdbcds to the SQLFILE appender and _only_ to the SQLFILE appender -->
<logger name="org.jdbcds" additivity="false">
    <appender-ref ref="SQLFILE" />
</logger>