0

I have added log4j 2.20.0 to a Maven project. The problem is that calling LogManager.getLogger(Main.class) takes several seconds to complete. About six seconds. Everywhere I look the preferred way to store a logger is to make the call once for each class and store it in a static class variable. The call will then more or less be the first thing that happens after launch and my application is blocked waiting for it to complete for six seconds. It hurts the development process and the user experience.

In running on a MacBook Air, M1 2020 (AArch64) - macOS Ventura 13.2.1


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>Example</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.20.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.20.0</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>

Main.java

package com.example;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.swing.JFrame;

public class Main {
    private static final Logger logger = LogManager.getLogger();

    public static void main(String[] args) {
        logger.info("application launched");
        var frame = new JFrame();
        frame.setSize(233, 377);
        frame.setVisible(true);
    }
}

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

I have tried running with versions 2.20.0, 2.19.0, 2.18.0, 2.17.0 but the problem remains.

mammut
  • 11
  • 3

1 Answers1

1

The problem is that when creating the logger it wants to set the property "hostName". In that process it will make a call to InetAddress.getLocalHost() which will take several seconds to complete.

A solution can be found here: InetAddress.getLocalHost() slow to run (30+ seconds)

mammut
  • 11
  • 3