java 无法初始化Log4j -SLF 4JLoggerContextFactory

jrcvhitl  于 2023-06-28  发布在  Java
关注(0)|答案(2)|浏览(134)

我用log4j2运行了一个jetty webapp。它不记录任何内容,并且在启动时出现以下错误:
ERROR StatusLogger LogManager返回了org.apache.logging.slf4j.SLF4JLoggerContextFactory的示例,该示例未实现org.apache.logging.log4j.core.impl.Log4jContextFactory。无法初始化Log4j。

我的登录码:

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>

怎么了?

qybjjes1

qybjjes11#

不太清楚是什么问题,但我认为这是一个与Sping Boot Logger和Log4J2的冲突。我设法解决了这个问题,排除了我的mavenpom.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>
lkaoscv7

lkaoscv72#

我也有同样的问题。原因是我在我的主类中配置了log4j,如下所示:

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

删除此手动配置后,启动错误消失。

相关问题