Search code examples
javalogginglogback

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>

Solution

  • 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 (e.g. 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!