Open eldwrjwt opened 2 days ago
Hi @eldwrjwt,
I believe this is because the requiresLocation function in FailoverAppender is the default implementation in AbstractAppender, which relies on the layout inside the appender. However, for FailoverAppender, we do not have a layout, and thus fail to generate the location field in the log event object.
Nice catch! :100: Can you submit a PR that fixes it?
Additionally, the Async logger uses a RingBufferLogEvent, which directly reads the location field, so there's no location to print.
The shouldn't be a problem:
o.a.l.l.spi.AbstractLogger
class from Log4j API, which populates the value of the location early:In both cases the location
field is populated if requiresLocation()
returns true
.
This issue is also true for AsyncAppender.
I agree, we have the same problem as in Failover
here: the AsyncAppender.requiresLocation()
implementation is incorrect.
Could you submit a separate PR for this one?
Note that AsyncAppender
has an additional twist to make things interesting: it has an includeLocation
configuration attribute that must be turned on to compute location. The implementation of requiresLocation()
for the Async appender should probably be: includeLocation
is true and any of the downstream appenders has requiresLocation() == true
.
Remark: If location information is important to you, since last year we have a Log4j Transform Maven plugin, which precomputes the location of your log statements at build time.
If the location is precomputed, it will be available regardless of any includeLocation
setting: these settings prevent the computation of location information at runtime, they don't delete the information that is already there.
Hi @ppkarwasz,
I can submit a PR to fix the issue later on. If my understanding is correct, I think I can write a correct implementation of requiresLocation()
for FailoverAppender
(and AsyncAppender
in another PR), and this should fix the bug.
Hi @ppkarwasz, I can submit a PR to fix the issue later on. If my understanding is correct, I think I can write a correct implementation of
requiresLocation()
forFailoverAppender
(andAsyncAppender
in another PR), and this should fix the bug.
That's great! Yes, requiresLocation()
is the method that needs fixing. Please add some unit tests too.
PR Submitted in #3259 for FailoverAppender
Another PR #3260 for AsyncAppender
Description
I was using a failover appender with an AsyncLoggerContextSelector and wanted to print the line number. I set includeLocation to true in the root logger, but there were still no line numbers in the logs. We encountered the same problem with the async appender, even with includeLocation set to true.
I believe this is because the requiresLocation function in FailoverAppender is the default implementation in AbstractAppender, which relies on the layout inside the appender. However, for FailoverAppender, we do not have a layout, and thus fail to generate the location field in the log event object. Additionally, the Async logger uses a RingBufferLogEvent, which directly reads the location field, so there's no location to print. This issue is also true for AsyncAppender.
Configuration
Version: 2.24.2
Operating system: Windows11
JDK: OpenJDK23
Reproduction
Log4j2.xml
log4j2.component.properties