[logback-dev] [JIRA] Updates for LOGBACK-1775: Last argument of log message stripped if Throwable, even when cause is given as well (Issue #876)
logback developers list
logback-dev at qos.ch
Sat Oct 19 17:43:00 UTC 2024
logback / LOGBACK-1775 [Open]
Last argument of log message stripped if Throwable, even when cause is given as well (Issue #876)
==============================
Here's what changed in this issue in the last few minutes.
This issue has been created
View or comment on issue using this link
https://jira.qos.ch/browse/LOGBACK-1775
==============================
Issue created
------------------------------
Ceki Gülcü created this issue on 19/Oct/24 17:32
Summary: Last argument of log message stripped if Throwable, even when cause is given as well (Issue #876)
Issue Type: Bug
Affects Versions: 1.5.8
Assignee: Ceki Gülcü
Components: logback-classic
Created: 19/Oct/24 17:32
Priority: Major
Reporter: Ceki Gülcü
Description:
If a Throwable type is passed to match the last {} argument, it is ignored, and the formatted log message will write {} instead.
{code:java}
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";
}
}
}
{code}
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.
==============================
This message was sent by Atlassian Jira (v9.6.0#960000-sha1:a3ee8af)
More information about the logback-dev
mailing list