如何在Logback中过滤堆栈跟踪帧

时间:2016-01-29 02:56:00

标签: java logging logback

假设我有两个Java类:foo.ClassAbar.ClassB。要打印(根)异常堆栈跟踪,我需要只打印foo包的帧。我的问题是如何配置Logback来做到这一点 我知道该功能已在Logback中实现,我需要使用evaluator。但我无法理解。我尝试了here描述的例子没有成功(毫不奇怪)。

任何人都可以提供过滤堆栈跟踪帧的确切配置吗?

<configuration>
  <evaluator name="FILTER">
    <expression>¿what should I put here?</expression>
  </evaluator>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <encoder>
      <pattern>[%thread] %-5level - %msg%n%rEx{full, FILTER}</pattern>
    </encoder>
  </appender>

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

3 个答案:

答案 0 :(得分:6)

我联系了Tomasz Nurkiewicz(问题中链接的博客的作者),他很友好地回答了我的问题。我发布了答案,以防万一:
在打印堆栈跟踪时,要过滤来自bar包的行,请使用以下命令:

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <encoder>
      <pattern>[%thread] %-5level - %msg%n%rEx{full, bar}</pattern>
    </encoder>
  </appender>
  ...
</configuration>

我想在我的Web应用程序(Tomcat + Spring + Hibernate +等)中使用它。所以我使用以下命令配置了logback,不打印org.something个包中的任何堆栈跟踪行(例如org.apacheorg.springframeworkorg.hibernate等):

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <encoder>
      <pattern>[%thread] %-5level - %msg%n%rEx{full, org}</pattern>
    </encoder>
  </appender>
  ...
</configuration>

感谢Tomasz!

答案 1 :(得分:1)

I wrote my own feature that filters stacktrace which I used since 2010 and I am very happy with it. I published my own Open Source Java Library called MgntUtils which has stackfiltering feature and some other useful (IMHO) features. Here is the link to maven central MgntUtils and here is a link to github if you want a source code: MgntUtils at github also here is the link to the article that describes the library: MgntUtils Open Source Java library with stack trace filtering, Silent String parsing, Unicode converter and Version comparison. See The paragraph "Stacktrace noise filter"

答案 2 :(得分:0)

这可能会帮助像我这样编写springboot应用程序的人

我当前正在使用

<configuration debug="true" packagingData="true">
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                myawesomeprojectname %d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger.%M:%L - %msg%n%rEx{full,java.lang.reflect.Method,
                org.apache.catalina,
                org.apache.tomcat,
                org.apache.coyote,
                javax,
                java.util.concurrent,
                java.lang.Thread,
                org.springframework.aop,
                org.springframework.boot.actuate,
                org.springframework.security,
                org.springframework.transaction,
                org.springframework.web,
                sun.reflect,
                net.sf.cglib,
                ByCGLIB
                }
            </pattern>
        </encoder>
    </appender>

    <root level="ERROR">
        <appender-ref ref="CONSOLE"/>
    </root>

    <logger name="org.apache.http" level="DEBUG"/>
    <logger name="com.gm" level="INFO"/>
    <logger name="org.springframework.core.env" level="INFO"/>
    <logger name="org.hibernate.type.descriptor.sql" level="ERROR" /><!-- TRACE will log params-->

</configuration>