Search code examples
javalog4j2

Unable to initialize Log4j - SLF4JLoggerContextFactory


I have a jetty webapp running with log4j2. It is not logging anything and there is the following error on startup:

ERROR StatusLogger LogManager returned an instance of org.apache.logging.slf4j.SLF4JLoggerContextFactory which does not implement org.apache.logging.log4j.core.impl.Log4jContextFactory. Unable to initialize Log4j.

My logging code:

org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getLogger(MyClass.class);
logger.info("something");

log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Properties>
        <property name="layout.pattern">%d %5p %c{1.} [%t] %m%n</property>
    </Properties>

    <Appenders>
        <Console name="consoleAppender">
            <PatternLayout pattern="${layout.pattern}" />
        </Console>

        <RollingFile name="rollingFileAppender">
            <FileName>logs/app.log</FileName>
            <FilePattern>logs/%d{yyyy-MM-dd}-app.log</FilePattern>
            <PatternLayout pattern="${layout.pattern}" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="250 MB" />
            </Policies>
            <DefaultRolloverStrategy fileIndex="nomax" />
        </RollingFile>
    </Appenders>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="consoleAppender" />
        </Root>
        <Logger name="com.app" level="all"
            additivity="false">
            <AppenderRef ref="consoleAppender" />
            <AppenderRef ref="rollingFileAppender" />
        </Logger>
    </Loggers>
</Configuration>

What is wrong?


Solution

  • not exactly sure what was the problem but i think it was a conflict with spring boot logger and log4j2. i managed to resolved the issue by exluding the dependency in my maven pom.xml:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>