如何在logback中过滤某些类型的异常

时间:2017-06-12 17:33:44

标签: logback

我使用了一些例外来处理一些验证逻辑。问题是带有logback的spring boot(1.5)默认记录所有异常。

您能否帮助我在logback.xml中添加要添加的属性。

1 个答案:

答案 0 :(得分:1)

在logback xml中添加带表达式的过滤器。下面你会找到logback xml。除此之外,你必须在你的pom.xml中添加janino依赖

<!-- logback -->
    <dependency>
            <groupId>org.codehaus.janino</groupId>
            <artifactId>janino</artifactId>
            <version>2.5.16</version>
        </dependency>


    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.1.2</version>
    </dependency>

logback.xml

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n
        </pattern>
    </encoder>
</appender>


<appender name="test" class="ch.qos.logback.core.ConsoleAppender">

    <encoder>
        <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg%n</pattern>

    </encoder>
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
        <evaluator>
            <expression>return  (message.contains("javax.management.InstanceAlreadyExistsException"));
            </expression>
        </evaluator>
        <OnMismatch>NEUTRAL</OnMismatch>
        <OnMatch>DENY</OnMatch>

    </filter>

</appender>



<logger name="com.logbacke.example"
    level="ERROR" additivity="false">

    <appender-ref ref="test" />
</logger>

<!-- By default, the level of the root level is set to DEBUG -->
<root level="DEBUG">
    <appender-ref ref="STDOUT" />
</root>