获取java.lang.IllegalStateException:检测到Logback配置错误错误

13

Logback在我的Spring Boot应用程序中抛出以下错误:

java.lang.IllegalStateException: 发现Logback配置错误: ch.qos.logback.core.joran.spi.Interpreter@3:16中的ERROR - [Appenders]没有适用的操作,当前ElementPath是[[Configuration][Appenders]] ch.qos.logback.core.joran.spi.Interpreter@4:53中的ERROR - [Console]没有适用的操作,当前ElementPath是[[Configuration][Appenders][Console]] ch.qos.logback.core.joran.spi.Interpreter@5:92中的ERROR - [PatternLayout]没有适用的操作,当前ElementPath是[[Configuration][Appenders][Console][PatternLayout]] ch.qos.logback.core.joran.spi.Interpreter@9:49中的ERROR - [RollingFile]没有适用的操作,当前ElementPath是[[Configuration][Appenders][RollingFile]] ch.qos.logback.core.joran.spi.Interpreter@10:28中的ERROR - [PatternLayout]没有适用的操作,当前ElementPath是[[Configuration][Appenders][RollingFile][PatternLayout]] ch.qos.logback.core.joran.spi.Interpreter@11:26中的ERROR - [pattern]没有适用的操作,当前ElementPath是[[Configuration][Appenders][RollingFile][PatternLayout][pattern]] ch.qos.logback.core.joran.spi.Interpreter@13:23中的ERROR - [Policies]没有适用的操作,当前ElementPath是[[Configuration][Appenders][RollingFile][Policies]] ch.qos.logback.core.joran.spi.Interpreter@14:59中的ERROR - [SizeBasedTriggeringPolicy]没有适用的操作,当前ElementPath是[[Configuration][Appenders][RollingFile][Policies][SizeBasedTriggeringPolicy]] ch.qos.logback.core.joran.spi.Interpreter@16:50中的ERROR - [DefaultRolloverStrategy]没有适用的操作,当前ElementPath是[[Configuration][Appenders][RollingFile][DefaultRolloverStrategy]] ch.qos.logback.core.joran.spi.Interpreter@20:14中的ERROR - [Loggers]没有适用的操作,当前ElementPath是[[Configuration][Loggers]] ch.qos.logback.core.joran.spi.Interpreter@21:29中的ERROR - [Root]没有适用的操作,当前ElementPath是[[Configuration][Loggers][Root]] ch.qos.logback.core.joran.spi.Interpreter@22:46中的ERROR - [AppenderRef]没有适用的操作,当前ElementPath是[[Configuration][Loggers][Root][AppenderRef]] ch.qos.logback.core.joran.spi.Interpreter@23:42中的ERROR - [AppenderRef]没有适用的操作,当前ElementPath是[[Configuration][Loggers][Root][AppenderRef]] ch.qos.logback.core.joran.spi.Interpreter@25:76中的ERROR - [Logger]没有适用的操作,当前ElementPath是[[Configuration][Loggers][Logger]] ch.qos.logback.core.joran.spi.Interpreter@26:44中的ERROR - [AppenderRef]没有适用的操作,当前ElementPath是[[Configuration][Loggers][Logger][AppenderRef]] ch.qos.logback.core.joran.spi.Interpreter@27:40中的ERROR - [AppenderRef]没有适用的操作,当前ElementPath是[[Configuration][Loggers][Logger][AppenderRef]] ch.qos.logback.core.joran.spi.Interpreter@29:68中的ERROR - [Logger]没有适用的操作,当前ElementPath是[[Configuration][Loggers][Logger]] ch.qos.logback.core.joran.spi.Interpreter@30:44中的ERROR - [AppenderRef]没有适用的操作,当前ElementPath是[[Configuration][Loggers][Logger][AppenderRef]] 在org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:162)中 在org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:66)中初始化 在org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:56)中初始化 在org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:115)中初始化 在org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:308)中初始化 在org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:276)中初始化 在org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent

我的logback配置如下:

<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <RollingFile name="RollingFile" fileName="${sys:home}/logs/log"
            filePattern="${sys:home}/logs/mylog-%i.log" bufferedIO="false"
            immediateFlush="true" append="true">
            <PatternLayout>
                <pattern>%d %p [%t] %c{1.} %m%n</pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
            <DefaultRolloverStrategy max="100" />
        </RollingFile>
    </Appenders>

    <Loggers>
        <Root level="error">
            <AppenderRef ref="RollingFile" />
            <AppenderRef ref="Console" />
        </Root>
        <Logger name="org.springframework" level="info" additivity="false">
            <AppenderRef ref="RollingFile"></AppenderRef>
            <AppenderRef ref="Console"></AppenderRef>
        </Logger>
        <Logger name="com.myproj" level="debug" additivity="false">
            <AppenderRef ref="RollingFile"></AppenderRef>
        </Logger>
    </Loggers>
</Configuration>

我不是100%确定,但看起来你的logback配置语法有误。请参考https://logback.qos.ch/manual/configuration.html。 - Nikolai Shevchenko
我认为这个主题会对你有所帮助: https://dev59.com/QloU5IYBdhLWcg3w9aai - Gabriel Da Silva Costa
当我指定一个无法访问的日志文件位置时,出现了这个错误。 - EM-Creations
2个回答

16

这可能是log4j2配置。为了支持它,您的Spring Boot pom文件必须按以下方式更新:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

点击此处查看更多详细信息:https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-logging(第8.2节:配置Log4j进行日志记录)。


遇到了处理logback的同样问题。对于我来说,添加当前版本的实现依赖项即可正常工作,无需设置排除项。 - MMascarin
@MMascarin - 能否详细解释一下做了什么?我已经遇到这个问题三天了,但是一直无法解决 :/ 请帮忙。 - Lisbon
我认为我刚刚尝试了mp31415的解决方案(只需单独声明spring-boot-starter-log4j2),并意识到实际上我不需要排除它才能工作。 - MMascarin
这拯救了我的起始项目,非常感谢! - sanketh s

1
错误可以通过使用以下方法解决:
<appender name="Console"
            class="ch.qos.logback.core.ConsoleAppender">

替代

<Console name="Console" target="SYSTEM_OUT">

以下是参考资料,您可以按照以下步骤进行操作:

参考链接

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan = "true">

        <property name="LOGS" value="./logs" />
        <property resource ="application.yml"/>
        <springProperty name="NAME" source="spring.application.name" />
        <appender name="Console"
            class="ch.qos.logback.core.ConsoleAppender">
            <layout class="ch.qos.logback.classic.PatternLayout">
                <Pattern>
                    %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
                </Pattern>
            </layout>
        </appender>
        
        <appender name="RollingFile"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS}/${NAME}.log</file>
        <encoder
            class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
        </encoder>

        <rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily and when the file reaches 10 MegaBytes -->
            <fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>
     
    <root level="info">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </root>

    <logger name="com.ms" level="trace" additivity="false">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </logger>
    
    <logger name="org.springframework.core.env.PropertySourcesPropertyResolver" level="trace" additivity="true">
    </logger>

</configuration>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接