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

Ralph Goers commented on LOG4J2-1094:
-------------------------------------

ClassLoaderContextSelector creates a LoggerContext when one does not exist. 
This LoggerContext initially has a NullConfiguration associated with it.  This 
LoggerContext is returned to Log4jContextFactory where it is then started, 
which causes it to be configured. It is possible that one thread might call 
getLogger() and cause the LoggerContext to be created. A second thread might 
then call getLogger and obtain the LoggerContext which has not yet been 
configured. The solution to this would be to synchronize the getContext method 
in Log4jContextFactory, but this would lead to very undesirable behavior as you 
could end up with contention on all calls to getLogger() - and it would cause 
the second thread to block while configuration was completed.

For these reasons I believe this behavior should be documented. As noted, the 
fix is to just call getLogger() before starting any threads.

> Multi thread initialization problem
> -----------------------------------
>
>                 Key: LOG4J2-1094
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1094
>             Project: Log4j 2
>          Issue Type: Bug
>    Affects Versions: 2.3
>            Reporter: Luca Burgazzoli
>
> I wrote a very simple example which has a behaviour I do not expect:
> If I call LogManager.getLogger(..) from two threads, only one of the loggers 
> logs what I'd expect but if I add an additional call to 
> LogManager.getLogger(..) before the threads are started, I see what I'd 
> expect so it looks like there is a problem in multi threaded initialization.
> You can fine the code and the configuration here:
> - 
> https://github.com/lburgazzoli/lb-chronicle/blob/master/chronicle-examples/chronicle-logger-log4j2/src/main/java/com.github.lburgazzoli.openhft.examples.chronicle.logger.log4j2/MtLogging.java
> - 
> https://github.com/lburgazzoli/lb-chronicle/blob/master/chronicle-examples/chronicle-logger-log4j2/src/main/resources/log4j2.xml
> The code above will show:
> {noformat}
>     write thread-1
>     done thread-1 
>     write thread-2
>     [TEST] [INFO ] thread-2 - message
>     done thread-2
> {noformat}
> Any call to LogManager makes it succeed:
> {code}
>     LogManager.getContext(false);
>     th1.start();
>     th2.start();
>     th1.join();
>     th2.join();
> {code}
> New output:
> {noformat}
>     write thread-2
>     write thread-1
>     [TEST] [INFO ] thread-2 - message
>    done thread-2
>    [TEST] [INFO ] thread-1 - message
>    done thread-1
> {noformat}
> The funny thing is that the first thread to arrive is initialized with ERROR 
> level instead of the ALL that is given to root. In other words it seems that 
> the config has not loaded



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-dev-h...@logging.apache.org

Reply via email to