logback日志打印过滤

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26917447/article/details/81011315

1.只打印sql语句以及参数

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <!-- 设置 logger context 名称,一旦设置不可改变,默认为default -->
    <!-- <contextName>myAppName</contextName> -->
    <timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss" />
    <!-- 日志文件大小最大值 -->
    <property name="maxFileSize" value="1kb" />
    <!-- LoggerContextListener 接口的实例能监听 logger context 上发生的事件,比如说日志级别的变化 -->
    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
        <resetJUL>true</resetJUL>
    </contextListener>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder 
            by default -->
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
                <expression>return message.contains("mapper") || message.contains("component") || message.contains("MapperFactoryBean");</expression>
            </evaluator>
            <OnMismatch>NEUTRAL</OnMismatch>
            <OnMatch>DENY</OnMatch>
        </filter>
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
                <expression>return message.contains("Preparing") || message.contains("Parameters:") || message.contains("Total:");</expression>
            </evaluator>
            <OnMismatch>DENY</OnMismatch>
            <OnMatch>ACCEPT</OnMatch>
        </filter>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <logger name="java.sql.Statement" level="DEBUG" />
    <logger name="java.sql.PreparedStatement" level="DEBUG" />
    <logger name="com.xxx" level="debug" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>

    <root>
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

上面有两个过滤器,
其中第一个过滤器是过滤掉所有包含字符串“mapper”,“component”,“MapperFactoryBean”的日志打印
第二个过滤器是只打印包含字符串”Preparing”,”Paramters:”,”Total:”的日志内容。

展开阅读全文

没有更多推荐了,返回首页