qos-ch / slf4j

Simple Logging Facade for Java
http://www.slf4j.org
MIT License
2.32k stars 980 forks source link

Wrong classname when using slf4j inside a wrapper and logging via LoggingEventBuilder #409

Closed MzrW closed 4 months ago

MzrW commented 5 months ago

I am using the kotlin-logging library which is basically a wrapper around slf4j tailored for kotlin. (Original issue kotlin-logging-416).

When logging using the LoggingEventBuilder and setting the CallerBoundary accordingly it prints the wrong classname.

package my.pkg;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.spi.CallerBoundaryAware;

public class Example {
    private static final LoggingWrapper wrapper = new LoggingWrapper();

    public static void main(String[] args) {
        wrapper.logWithEvent("hello");
    }
}

class LoggingWrapper {
    private Logger underlyingLogger = LoggerFactory.getLogger("anything");

    public void logWithEvent(String msg) {
        var builder = underlyingLogger.atInfo();
        // setting the caller boundary to LoggingWrapper
        if(builder instanceof CallerBoundaryAware)
            ((CallerBoundaryAware) builder).setCallerBoundary(LoggingWrapper.class.getName());
        builder.log(msg);
    }
}

Running the given example I expect the following output:

Apr. 06, 2024 6:28:12 AM my.pkg.Example main
INFORMATION: hello

However following is printed:

Apr. 06, 2024 6:28:12 AM my.pkg.LoggingWrapper logWithEvent
INFORMATION: hello

Checkout the example: example.zip

ceki commented 4 months ago

Linked to SLF4J-601 jira issue

ceki commented 4 months ago

@MzrW Thank you for reporting this issue and the accompanying PR.

I have made several changes inspired by your PR without merging it. Please provide your full name so that you can be given credit.

In any case, the issue is fixed in commit 3386517ad6 part of slf4j version 2.0.13 released today.

MzrW commented 4 months ago

@ceki Thank you for the fix, I could verify it's working properly. My full name is Moritz Waser.

MzrW commented 4 months ago

fixed in version 2.0.13

ceki commented 4 months ago

I added your name in https://www.slf4j.org/news.html#2.0.13

By the way, compliments on your PR. Excellent work.

m-waser commented 4 months ago

thank you :)