[ 
https://issues.apache.org/jira/browse/LOG4J2-643?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14005482#comment-14005482
 ] 

Remko Popma commented on LOG4J2-643:
------------------------------------

If you make all loggers asynchronous by setting system property 
{{Log4jContextSelector}} to 
{{org.apache.logging.log4j.core.async.AsyncLoggerContextSelector}}, there are 
no new log events created for each log message: the ringbuffer is pre-allocated 
with RingBufferLogEvents which are reused. 
So this is a different mechanism where LogEventFactory does not come into play.

There is an alternative though: you can use what the documentation calls 
["Mixing Synchronous and Asynchronous 
Loggers"|http://logging.apache.org/log4j/2.x/manual/async.html#MixedSync-Async].
 This has a different ringbuffer and does use the LogEventFactory to create log 
events, so the solution you mention to always put values in the MDC should work.

To do this, remove the  {{Log4jContextSelector}} system property and instead 
change your log4j2.xml configuration: replace {{<Logger>}} by {{<AsyncLogger>}} 
and replace {{<Root>}} by {{<AsyncRoot>}}. Performance-wise this is fairly 
close to making all loggers asynchronous (performance comparison is on the same 
doc page).

Please take a look at the docs and let me know if you run into any problems.

> LogEventFactory not called when using AsyncContextSelector
> ----------------------------------------------------------
>
>                 Key: LOG4J2-643
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-643
>             Project: Log4j 2
>          Issue Type: Improvement
>          Components: Core
>    Affects Versions: 2.0-rc2
>            Reporter: Stefan Wehner
>            Priority: Minor
>
> We're using the JDBCAppender to write log messages to a MySQL database - 
> including some data provided by the ThreadContext.
> We wanted to make sure that at least some default values are available in the 
> Event's MDC in case the values are not provided in the ThreadContext We also 
> calculate some extra data (delta of the log event's time from the request 
> start time - stored in the ThreadContext).
> We thought a good idea would be to do this in a custom LogEventFactory, that 
> makes sure that values are set, provides defaults, and calculates the offset.
> When switching to using the AsyncLoggerContext this doesn't work anymore, 
> because the LogEventFactory is not used (unless the RingBuffer is full - see 
> AsyncLogger:248).
> I'm not sure if we're abusing the LogEventFactory here for something that it 
> wasn't meant to do...
> If you think this is a legit use case: shouldn't the async logging also use 
> this factory somehow? I understand the biggest problem of using the 
> LogEventFactory to create an event, and then pass the values to the 
> translator would be the context stack, which is handled differently in the 
> AsyncLogger and the Log4jLogEvent constructor.



--
This message was sent by Atlassian JIRA
(v6.2#6252)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to