I'm using logback 1.1.3 over slf4j-api 1.7.7.
I'm reading the following documentation here.
The code (which I can't modify) that I'm trying to restrict the logging logs and throws exceptions in the following way.
try {.. }
catch( Exception e ) {
log.error( "Houston we have a problem", e );
throw new TestException( "Houston we have a problem", e );
}
The log format I'm using is the following.
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
And I want some loggers to restrict the display of their stacktrace to just 2 lines so I thought I could simply modify that to the following format as stipulated in the documentation.
<appender name="STDOUT_COMPACT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%ex{2} %d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
I was expecting to get something like the following.
mainPackage.foo.bar.TestException: Houston we have a problem
at mainPackage.foo.bar.TestThrower.fire(TestThrower.java:22)
But instead, I was getting the following.
mainPackage.foo.bar.TestException: Houston we have a problem
at mainPackage.foo.bar.TestThrower.fire(TestThrower.java:22)
mainPackage.foo.bar.TestException: Houston we have a problem
at mainPackage.foo.bar.TestThrower.fire(TestThrower.java:22)
at mainPackage.foo.bar.TestThrower.fire1(TestThrower.java:12)
at mainPackage.foo.bar.TestThrower.fire2(TestThrower.java:44)
at mainPackage.foo.bar.TestThrower.fire3(TestThrower.java:122)
at mainPackage.foo.bar.TestThrower.fire4(TestThrower.java:322)
at mainPackage.foo.bar.TestThrower.fire5(TestThrower.java:72)
..
The log was simply repeated instead of being truncated to just 2 lines. What am I doing wrong? How exactly do I use the ex{n}
format appropriately? I don't prefer to write my custom logger for this.