Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 991
Rad Avatar asked Jan 29 '16 02:01

Rad


Video Answer


1 Answers

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

like image 78
Kalpesh Soni Avatar answered Oct 06 '22 10:10

Kalpesh Soni