Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback - filtering stack trace

I'm using logback for logging and i found some problem with filtering stack trace. I hava a structure like this:

public class Main {
    static final Logger logger = (Logger) LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {
        logger.debug("Start");

        MyObject1 loggingElement = new MyObject1();
        loggingElement.logg();

        logger.debug("End");
    }
}


public class MyObject1 {

    public MyObject2 obj;
    static final Logger logger = (Logger) LoggerFactory.getLogger(MyObject1.class);

    public MyObject1() {
        obj = new MyObject2();
    }
    public void logg() {
        obj.loggError();
    }
}
public class MyObject2 {
    static final Logger logger = (Logger) LoggerFactory.getLogger(MyObject2.class);

    public void loggError() {
        logger.error("Error info", new Throwable("Error"));
    }
}

and configuration xml like this:

<property name="mask" 
            value="MyObject2"/>


<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss} | %level | %-4thread | %-21logger | %m%n
        </pattern>
    </encoder>
</appender>

<root level="${root.level:-TRACE}">
    <appender-ref ref="STDOUT" />
</root>

When i run the main method i get output like this:

12:29:35 | DEBUG | main | com.logging.Main      | Start
12:29:35 | ERROR | main | com.logging.MyObject2 | Error info
java.lang.Throwable: Error
    at com.logging.MyObject2.loggError(MyObject2.java:11) [bin/:na]
    at com.logging.MyObject1.logg(MyObject1.java:17) [bin/:na]
    at com.logging.Main.main(Main.java:14) [bin/:na]
12:29:35 | DEBUG | main | com.logging.Main      | End

i would like to remove line with 'MyObject1' in it and leave rest of stack trace untouched just like int this example: http://java.dzone.com/articles/filtering-stack-trace-hell

i tried to change configuration of logback

<pattern>%d{HH:mm:ss} | %level | %-4thread | %-21logger | %m%n%ex{full,${mask}}
</pattern>

doesn't work at all

<pattern>%d{HH:mm:ss} | %level | %-4thread | %-21logger | %m%n%eXe{full,${mask}}
            </pattern>

removes whole stack trace(not solution for me)

Anyone knows anything that could help?

like image 640
viniolli Avatar asked Jul 19 '12 11:07

viniolli


2 Answers

This capability has been included in Logback Classic as of version 1.1.3. It seems to apply to the following Conversion Words of PatternLayout:

  • ex / exception / throwable
  • rEx / rootException
  • xEx / xException / xThrowable
like image 73
E-Riz Avatar answered Oct 13 '22 01:10

E-Riz


On my case I used a ThrowableConverter for the stacktrace.

You can add that to your logback.xml where you have your consoleApender, inside your encoder:

    <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">

            <!-- your other encoder configurations -->

            <stackTrace>
                <fieldName>stacktrace</fieldName>
                <throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
                    <pattern>[%thread] - %msg%n%stack{1,1024,10,rootFirst}</pattern>
                </throwableConverter>
            </stackTrace>

        </encoder>
    </appender>

It will add a field name "stack_trace" with the stack trace. And it does the job for me.

Inside stack you have the parameters:

  • maxDepthPerThrowable: Limits the number of stackTraceElements per throwable
  • maxLength: Limits the total length in characters of the trace.
  • shortenedClassNameLength: Abbreviates class names based on the shortenedClassName
  • rootCauseFirst: Outputs in either 'normal' order (root-cause-last), or root-cause-first
like image 45
Sylhare Avatar answered Oct 12 '22 23:10

Sylhare