简体   繁体   中英

How to filter stacktrace frames in Logback

Say I have two Java classes: foo.ClassA and bar.ClassB . To print (root) exception stacktrace, I need to print only frames of foo package. My question is how exactly I can configure Logback to do that.
I know that the feature is already implemented in Logback, and I need to use evaluator . But I couldn't figure it out. I tried the example described here without success (no surprise).

Can anyone give the exact configuration for filtering stacktrace frames?

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

I contacted Tomasz Nurkiewicz (the author of the blog linked in the question) and he kindly answered my question. I'm posting the answer, just in case:
While printing stack trace, to filter lines from bar package use the following:

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

I wanted to use it in my web application (Tomcat+Spring+Hibernate+etc). So I configured logback with the following not to print any stack trace line from org.something packages (eg org.apache , org.springframework , org.hibernate , etc):

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

Thanks Tomasz!

This may help someone who is writing springboot apps like me

I am currently using

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" packagingData="true">
<springProperty scope="context" name="APP_NAME" source="spring.application.name"/>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<springProfile name="!cloud">

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${APP_NAME} %green(%d{dd-MM-yyyy HH:mm:ss.SSS}) %magenta([%thread]) %highlight(%-5level) %red(%logger.%M:%L) - %magenta(%msg) %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
                }%n
            </pattern>
        </encoder>
    </appender>
</springProfile>
<springProfile name="cloud">
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <!-- send cloud event in one line so kibana can log it as one event -->
            <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger.%M:%L - %msg %replace(%rEx){'[\r\n]+', '\\n'}%nopex %n</pattern>
        </encoder>
    </appender>
</springProfile>


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

<logger name="com.aaa" level="DEBUG"/>
<logger name="org.springframework.web.filter" level="INFO"/>
<logger name="org.springframework.cloud" level="INFO"/>
<logger name="org.springframework.core.env" level="INFO"/>
<logger name="org.springframework.boot" level="INFO"/>
<logger name="org.springframework.boot.actuate" level="WARN"/>
<logger name="org.springframework.boot.context" level="INFO"/>
<logger name="org.springframework.boot.autoconfigure" level="INFO"/>
<logger name="io.lettuce.core.protocol" level="WARN"/>

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM