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

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

[~vampire] As a general rule that just isn't true. Unless you are testing the 
logging framework itself there is rarely a need to have separate LoggerContexts 
per test method. If all you are trying to do is ensure you can identify logs 
for each test independent of the others you can use a key and value in the 
ThreadContextMap to do that. The make sure that key is included in your logging 
pattern. Even in many of Log4j's tests we utilize the same configuration for 
all the tests so having a different LoggerContext isn't required. We do have 
some tests that require their own LoggerContext but those ensure the 
LoggerContext is created before each test and shutdown after. An example that 
uses multiple configurations on the same test is 
[https://github.com/apache/logging-log4j2/blob/2.x/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/RandomAccessFileAppenderTest.java.]
  
[https://github.com/apache/logging-log4j2/blob/2.x/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/CompositeConfigurationTest.java]
 has multiple test methods each with their own LoggerContext.  Note that the 
issue with this last test is that each of the LoggerContexts is using the 
default ContextSelector so, since they all use the same ClassLoader, they would 
share the same LoggerContext were they to run on different threads at the same 
time. Hence my statement that a JunitLoggerContextSelector would solve this 
problem.

> 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
>            Assignee: Ralph Goers
>            Priority: Major
>
> 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 find 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
> {code}
> public class MtLogging {
>     public static void main(final String[] args) throws Exception {
>         //LogManager.getLogger("main");
>         Thread th1 = new Thread(() -> {
>             final String name = "thread-1";
>             final Logger log = LogManager.getLogger(name);
>             System.out.println("write " + name);
>             log.info("message");
>             System.out.println("done " + name);
>         });
>         Thread th2 = new Thread(() -> {
>             final String name = "thread-2";
>             final Logger log = LogManager.getLogger(name);
>             System.out.println("write " + name);
>             log.info("message");
>             System.out.println("done " + name);
>         });
>         th1.start();
>         th2.start();
>         th1.join();
>         th2.join();
>     }
> }
> {code}
> {code}
> <?xml version="1.0" encoding="UTF-8"?>
> <configuration>
>     <appenders>
>         <Console name="STDOUT" target="SYSTEM_OUT">
>             <PatternLayout pattern="[TEST] [%-5p] %c - %m%n%throwable{none}"/>
>         </Console>
>     </appenders>
>     <loggers>
>         <root level="all">
>             <appender-ref ref="STDOUT"/>
>         </root>
>     </loggers>
> </configuration>
> {code}
> 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 (the problem no longer occurs):
> {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
(v8.20.10#820010)

Reply via email to