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

Wrong class name printed when using LoggingEventBuilder with payload #416

Closed
MzrW opened this issue Apr 6, 2024 · 10 comments
Closed

Wrong class name printed when using LoggingEventBuilder with payload #416

MzrW opened this issue Apr 6, 2024 · 10 comments

Comments

@MzrW
Copy link

MzrW commented Apr 6, 2024

The wrong class name is printed when using the LoggingEventBuilder with a payload.

The following example demonstrates the issue:

package my.pkg

import io.github.oshai.kotlinlogging.KotlinLogging

private val logger = KotlinLogging.logger {  }

fun main() {
    logger.atInfo {
        message = "hi"
        payload = mapOf("k" to "v")
    }

    logger.atInfo {
        message = "hi"
    }
}

Which will print:

Apr. 06, 2024 4:49:02 AM io.github.oshai.kotlinlogging.slf4j.internal.LocationAwareKLogger logWithPayload
INFORMATION: k=v hi
Apr. 06, 2024 4:49:02 AM my.pkg.MyClassKt main
INFORMATION: hi

The first log statement will be logged with io.github.oshai.kotlinlogging.slf4j.internal.LocationAwareKLogger as a classname.
Which is wrong. It supposed to be my.pkg.MyClassKt.

In the second log statement however it seems to work fine.

Check out the example to reproduce the issue: example.zip

Copy link

github-actions bot commented Apr 6, 2024

Thank you for reporting an issue. See the wiki for documentation and slack for questions.

@MzrW
Copy link
Author

MzrW commented Apr 6, 2024

The difference seems to be inside LocationAwareKLogger.

Log statements without payload are logged using: org.slf4j.spi.LocationAwareLogger.log(org.slf4j.Marker marker, String fqcn, int level, String message, Object[] argArray, Throwable t)

Log statements with payload are logged using:
org.slf4j.spi.LoggingEventBuilder.log(String message).
LocationAwareKLoggger.logWithPayload seems to correctly set the callerBoundary.

The first thing org.slf4j.spi.DefaultLoggingEventBuilder.log(String message) does is to override the callerBoundary with its own classname.

Therefore I think the bug is inside slf4j not in kotlin-logging.

Maybe leave the issue open until the bug is resolved there ...

@oshai
Copy link
Owner

oshai commented Apr 6, 2024

Are you using log4j? There was a related bug there.

@MzrW
Copy link
Author

MzrW commented Apr 6, 2024

No Im using slf4j-jdk the java util logging.

@oshai
Copy link
Owner

oshai commented Apr 6, 2024

Log4j slf4j issue is here for reference: apache/logging-log4j2#1533

@oshai
Copy link
Owner

oshai commented Apr 6, 2024

It might be a similar fix.

@oshai
Copy link
Owner

oshai commented Apr 14, 2024

Seems like this should be fixed in slf4j 2.0.13. I will try to upgrade.

oshai added a commit that referenced this issue Apr 14, 2024
@oshai
Copy link
Owner

oshai commented Apr 14, 2024

Seems like this should be fixed in slf4j 2.0.13. I will try to upgrade.

should be in version 6.0.9.

@MzrW
Copy link
Author

MzrW commented Apr 16, 2024

When using slf4j version 2.0.13 the correct class name is logged.
@oshai feel free to close the issue.

@oshai
Copy link
Owner

oshai commented Apr 16, 2024

Thanks!

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

No branches or pull requests

2 participants