Unable to initialize Log4j - SLF4JLoggerContextFactory
Asked Answered
H

2

6

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?

Hunyadi answered 4/1, 2019 at 10:6 Comment(0)
H
8

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>
Hunyadi answered 9/1, 2019 at 10:57 Comment(0)
N
0

I had the same issue. The cause was that I configured log4j within my main class like this:

org.apache.logging.log4j.core.config.Configurator.initialize( "log4j2.xml", configLocation );

After removing this manual configuration the startup error disappeared.

Novelistic answered 18/3, 2020 at 19:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.