Skip to content

Log4j 1.2 API initialization does not respect log4j1.compatibility setting #3667

Open
@rschmitt

Description

@rschmitt

Description

I've come across a bug where a call to org.apache.log4j.LogManager.getLogger() exhibits apparently unintended side effects on the configuration (particularly log levels) of other loggers that were created previously. The below code prints two INFO-level log messages on Log4j 2.22.1 and earlier, but since 2.23.0 only the first message is emitted:

        Logger logger = (Logger) LogManager.getLogger(App.class);
        TestAppender testAppender = new TestAppender();

        logger.addAppender(testAppender);
        logger.setLevel(Level.INFO);
        testAppender.start();

        logger.info("Initializing Log4j 1.2 API...");
        org.apache.log4j.LogManager.getLogger("org.example.OtherClass");
        logger.info("Log4j 1.2 API initialized.");

        System.out.printf("Recorded %,d events (expected 2)%n", testAppender.getLogEvents());

Configuration

Version: 2.23.0 and later

JDK: Corretto 17.0.15

Logs

[0] ~/log4j-repro # ./run.sh
Testing 2.20.0
19:12:51.494 [main] INFO  org.example.App - Initializing Log4j 1.2 API...
19:12:51.502 [main] INFO  org.example.App - Log4j 1.2 API initialized.
Recorded 2 events (expected 2)

Testing 2.21.0
19:12:53.067 [main] INFO  org.example.App - Initializing Log4j 1.2 API...
19:12:53.075 [main] INFO  org.example.App - Log4j 1.2 API initialized.
Recorded 2 events (expected 2)

Testing 2.21.1
19:12:54.209 [main] INFO  org.example.App - Initializing Log4j 1.2 API...
19:12:54.217 [main] INFO  org.example.App - Log4j 1.2 API initialized.
Recorded 2 events (expected 2)

Testing 2.22.0
19:12:55.348 [main] INFO  org.example.App - Initializing Log4j 1.2 API...
19:12:55.356 [main] INFO  org.example.App - Log4j 1.2 API initialized.
Recorded 2 events (expected 2)

Testing 2.22.1
19:12:56.534 [main] INFO  org.example.App - Initializing Log4j 1.2 API...
19:12:56.544 [main] INFO  org.example.App - Log4j 1.2 API initialized.
Recorded 2 events (expected 2)

Testing 2.23.0
19:12:58.049 [main] INFO  org.example.App - Initializing Log4j 1.2 API...
Recorded 1 events (expected 2)

Testing 2.23.1
19:12:59.140 [main] INFO  org.example.App - Initializing Log4j 1.2 API...
Recorded 1 events (expected 2)

Testing 2.24.0
19:13:00.573 [main] INFO  org.example.App - Initializing Log4j 1.2 API...
Recorded 1 events (expected 2)

Testing 2.24.1
19:13:01.686 [main] INFO  org.example.App - Initializing Log4j 1.2 API...
Recorded 1 events (expected 2)

Testing 2.24.2
19:13:02.762 [main] INFO  org.example.App - Initializing Log4j 1.2 API...
Recorded 1 events (expected 2)

Testing 2.24.2
19:13:03.657 [main] INFO  org.example.App - Initializing Log4j 1.2 API...
Recorded 1 events (expected 2)

Testing 2.25.0-SNAPSHOT
2025-05-16T02:13:04.911900Z main INFO Initializing Log4j 1.2 API...Recorded 1 events (expected 2)

Reproduction

I made a standalone reproducer here. The run.sh script will run it against the last dozen releases of Log4j2, including the 2.25.0-SNAPSHOT release in the local Maven repository (in other words, the bug appears to be present as of the latest commit on 2.x).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIncorrect, unexpected, or unintended behavior of existing code

    Type

    Projects

    Status

    Ready

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions