flogger icon indicating copy to clipboard operation
flogger copied to clipboard

Incorrect line number when using Slf4jLoggerBackend

Open MosheElisha opened this issue 5 years ago • 7 comments

Hi,

I'm using Flogger with slf4j+log4j2 backend and a pattern that includes the line number (%line). The line number in the log message does not match my code but instead matches the log statements inside Slf4jLoggerBackend https://github.com/google/flogger/blob/2a6702386859e94c274c79dfad5830d36455d25b/slf4j/src/main/java/com/google/common/flogger/backend/slf4j/Slf4jLoggerBackend.java#L196

Attaching a small project that reproduces the issue.

line-number-test.zip

Output:

2020-03-17T17:10:53,709 [main] INFO  example.LogTest:208 - Info
2020-03-17T17:10:53,712 [main] WARN  example.LogTest:211 - Warn
2020-03-17T17:10:53,712 [main] ERROR example.LogTest:214 - Severe

The expectation is that the line number will match the log statement in the LogTest. Accurate line number is very important in our log messages.

Thanks.

MosheElisha avatar Mar 17 '20 15:03 MosheElisha

I'm afraid the SLF4J backend was a community contributed backend and isn't supported directly by Google. If you want to dig into the code and look for a solution I can probably answer some questions, or perhaps you can reach out to the original author ( https://github.com/cslee00 )

hagbard avatar Mar 24 '20 12:03 hagbard

Originally authored the SLF4J back-end. Due to the SLF4j API the Flogger back-end simply uses SLF4J in the same manner as calling code would, e.g. obtaining loggers via LoggerFactory.getLogger. The SLF4J API doesn't offer a means to provide call-site information - for SLF4J that's a back-end consideration. What SLF4J binding is in use?

If the configured SLF4J back-end supports configuration of call-site resolution it should be setup to grab callers of com.google.common.flogger.FluentLogger#forEnclosingClass (or your custom Flogger logger class, if any).

Chris.

On Tue, 24 Mar 2020 at 05:31, David Beaumont [email protected] wrote:

I'm afraid the SLF4J backend was a community contributed backend and isn't supported directly by Google. If you want to dig into the code and look for a solution I can probably answer some questions, or perhaps you can reach out to the original author ( https://github.com/cslee00 )

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/google/flogger/issues/140#issuecomment-603211370, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABCCXPQE2CZ6BF6IIM2C53TRJCR2ZANCNFSM4LNRE6SA .

cslee00 avatar Mar 24 '20 13:03 cslee00

Hi,

Thanks for the replies. I'm using log4j2 - log4j-slf4j-impl version 2.13.1

MosheElisha avatar Mar 24 '20 14:03 MosheElisha

""If the configured SLF4J back-end supports configuration of call-site resolution it should be setup to grab callers of com.google.common.flogger.FluentLogger#forEnclosingClass (or your custom Flogger logger class, if any).""

That's probably not a great way to do this. You're better off (if the API allows) to use the LogSite that's available in the LogData. If the API doesn't allow it, then would you be better using the Flogger backend that talks directly to the backend you have (e.g. Log4J2).

The logger that logs things and the classes the log statements are in are not necessarily the same.

hagbard avatar Mar 24 '20 15:03 hagbard

Hi,

If the API doesn't allow it, then would you be better using the Flogger backend that talks directly to the backend you have (e.g. Log4J2).

I see that log4j2 was added recently. I prefer to use log4j2 directly if possible but I noticed some issue with that as well (Opened issue https://github.com/google/flogger/issues/144).

Thanks!

MosheElisha avatar Mar 24 '20 22:03 MosheElisha

I'll personally not work on this (no time) but I will see about looking into the Log4j2 issue and see if that's easy enough.

hagbard avatar Mar 25 '20 14:03 hagbard

@hagbard Thank you. I appreciate that.

MosheElisha avatar Mar 26 '20 23:03 MosheElisha