Logback - omit frames when limiting stack trace depth

Viewed 56

I'm playing around with the logback patterns. I created a pattern that only prints the first three lines of the stack trace.

Normally logback omitts duplicate stack traces. But when I use the ex{depth} pattern to limit the stack trace lines it does not. I couldn't find a configuration property for this.

Any idea how I could omit duplicate stack trace lines when using the line-limit?

I'm using logback 1.2.11. Logback docs: https://logback.qos.ch/manual/layouts.html

Simple example:

Java Class:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestLog {

    private static final Logger LOGGER = LoggerFactory.getLogger(TestLog.class);

    public static void main(String[] args) {
        try {
            ex1();
        } catch (Exception e) {
            LOGGER.debug("foobar", e);
        }
    }

    static void ex1() {
        ex2();
    }

    static void ex2() {
        ex3();
    }

    static void ex3() {
        ex4();
    }

    static void ex4() {
        throw new IllegalStateException("ex in 4", new IllegalArgumentException("cause of ex in 4"));
    }

}

Logback config. Change the pattern for testing:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <!-- pattern to limit stack-trace-lines per exception to three, but duplicate frames are not omitted -->
<!--            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%15.15thread] %-20.20logger{19} : %m %ex{3}%nopex%n</Pattern>-->
            <!-- default pattern, omitts duplicate frames -->
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%15.15thread] %-20.20logger{19} : %m%n</Pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

Prints this in the default config:

2022-07-29 10:38:08.184 DEBUG [           main] TestLog              : foobar
java.lang.IllegalStateException: ex in 4
    at TestLog.ex4(TestLog.java:29)
    at TestLog.ex3(TestLog.java:25)
    at TestLog.ex2(TestLog.java:21)
    at TestLog.ex1(TestLog.java:17)
    at TestLog.main(TestLog.java:10)
Caused by: java.lang.IllegalArgumentException: cause of ex in 4
    ... 5 common frames omitted

Prints this, when limit is enabled:

2022-07-29 10:37:45.002 DEBUG [           main] TestLog              : foobar java.lang.IllegalStateException: ex in 4
    at TestLog.ex4(TestLog.java:29)
    at TestLog.ex3(TestLog.java:25)
    at TestLog.ex2(TestLog.java:21)
Caused by: java.lang.IllegalArgumentException: cause of ex in 4
    at TestLog.ex4(TestLog.java:29)
    at TestLog.ex3(TestLog.java:25)
    at TestLog.ex2(TestLog.java:21)
0 Answers
Related