[slf4j-dev] [Bug 223] New: Race-conditions initializing LoggerFactory

bugzilla-daemon at pixie.qos.ch bugzilla-daemon at pixie.qos.ch
Fri Jun 17 03:31:35 CEST 2011


http://bugzilla.slf4j.org/show_bug.cgi?id=223

           Summary: Race-conditions initializing LoggerFactory
           Product: SLF4J
           Version: 1.6.x
          Platform: PC
        OS/Version: Windows NT
            Status: NEW
          Severity: critical
          Priority: P1
         Component: Core API
        AssignedTo: slf4j-dev at qos.ch
        ReportedBy: cowwoc at bbs.darktech.org


1. Given:

public class MyTest
{
  private final Logger log = LoggerFactory.getLogger(MyTest.class);

  @Test
  public void firstMethod()
  {...}

  @Test
  public void secondMethod()
  {...}

  //etc
}

2. Run the unit test through Surefire 2.8.1 or newer (I am using
2.8.2-SNAPSHOT) with <parallel>methods</parallel> and JUnit 4.8.2
3. slf4j will issue the following warning:

SLF4J: The following loggers will not work because they were created
SLF4J: during the default configuration phase of the underlying logging system.
SLF4J: See also http://www.slf4j.org/codes.html#substituteLogger

4. MyTest is instantiated multiple times simultaneously, each time invoking
LoggerFactory.getLogger().

5. The first call begins invoking performInitialization(). The subsequent calls
return TEMP_FACTORY. As a result, the unit tests run with a misconfigured
logger.

Expected behavior: The first thread should run performInitialization() while
subsequent threads block waiting for initialization to complete.

I believe you can do this (and still allow re-entrant behavior) by using a
"synchronized (LoggerFactory.class)" block.

Workaround: Insert the following method once per class file:

    @BeforeClass
    public static void initSlf4j()
    {
        // Configure slf4j in a thread-safe manner, otherwise we get:
        //
        // SLF4J: The following loggers will not work because they were created
        // SLF4J: during the default configuration phase of the underlying
logging system.
        // SLF4J: See also http://www.slf4j.org/codes.html#substituteLogger
        LoggerFactory.getILoggerFactory();
    }

Though I don't think this workaround will work for <parallel>class</parallel>
or <parallel>both</parallel>.

-- 
Configure bugmail: http://bugzilla.slf4j.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.


More information about the slf4j-dev mailing list