Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last argument of log message stripped if Throwable, even when cause is given as well #876

Open
Maldivia opened this issue Oct 19, 2024 · 3 comments
Assignees
Labels
Milestone

Comments

@Maldivia
Copy link

Maldivia commented Oct 19, 2024

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.

@ceki
Copy link
Member

ceki commented Oct 19, 2024

Linked to https://jira.qos.ch/browse/LOGBACK-1775

@ceki
Copy link
Member

ceki commented Oct 19, 2024

@Maldivia

Thank you for this report. Issue fixed in commit 85968fa

Please note that log.makeLoggingEventBuilder(Level.WARN) is a longer form for log.atWarn().

@ceki ceki added the DONE label Oct 19, 2024
@ceki ceki self-assigned this Oct 19, 2024
@ceki ceki added this to the 1.5.12 milestone Oct 19, 2024
@Maldivia
Copy link
Author

Maldivia commented Oct 21, 2024

Please note that log.makeLoggingEventBuilder(Level.WARN) is a longer form for log.atWarn().

Ah ok - mostly used the builder to show the issues was also present when setCause was called explicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants