If a Throwable type is passed to match the last {} argument, it is ignored, and the formatted log message will write {} instead.
Example:
package com.test;
import ch.qos.logback.core.Appender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
public class Test {
static final Logger log = LoggerFactory.getLogger(Test.class);
public static void main(String[] args) {
log.info("SLF4J Version: {}", Logger.class.getPackage().getImplementationVersion());
log.info("Logback Version: {}", Appender.class.getPackage().getImplementationVersion());
Exception ex = new CustomException("Some message");
log.error("Exception Message: {}", ex, ex);
log.makeLoggingEventBuilder(Level.WARN)
.setMessage("Exception Message: {}")
.addArgument(ex)
.setCause(ex)
.log();
}
static class CustomException extends Exception {
public CustomException(String msg) {
super(msg);
}
@Override
public String toString() {
return "Custom Message";
}
}
}
Expected output:
[main] INFO com.test.Test -- SLF4J Version: 2.0.15
[main] INFO com.test.Test -- Logback Version: 1.5.11
[main] ERROR com.test.Test -- Exception Message: Custom Message
com.test.Test$CustomException: Some message
at com.test.Test.main(Test.java:15)
[main] WARN com.test.Test -- Exception Message: Custom Message
com.test.Test$CustomException: Some message
at com.test.Test.main(Test.java:15)
Actual output:
[main] INFO com.test.Test -- SLF4J Version: 2.0.15
[main] INFO com.test.Test -- Logback Version: 1.5.11
[main] ERROR com.test.Test -- Exception Message: {}
com.test.Test$CustomException: Some message
at com.test.Test.main(Test.java:15)
[main] WARN com.test.Test -- Exception Message: {}
com.test.Test$CustomException: Some message
at com.test.Test.main(Test.java:15)
Workaround:
Explicitly calling .toString() on the argument (log.error("Exception Message: {}", ex.toString(), ex)), or adding a bogus extra "" argument (log.error("Exception Message: {}", ex, "", ex)) works as a work around, but causes static analysis tools, including the one in IntelliJ, to warn about it.
If a
Throwabletype is passed to match the last{}argument, it is ignored, and the formatted log message will write{}instead.Example:
Expected output:
Actual output:
Workaround:
Explicitly calling
.toString()on the argument (log.error("Exception Message: {}", ex.toString(), ex)), or adding a bogus extra""argument (log.error("Exception Message: {}", ex, "", ex)) works as a work around, but causes static analysis tools, including the one in IntelliJ, to warn about it.