【问题标题】:Logging system failed to initialize using configuration from 'null' java.lang.IllegalStateException: Logback configuration error detected:日志系统无法使用来自“null”的配置进行初始化 java.lang.IllegalStateException:检测到 Logback 配置错误:
【发布时间】:2022-01-23 12:04:01
【问题描述】:

我想在我的 logback-spring.xml 中使用一个通用的 xml?如何在 logback-spring.xml 中包含 xml 而不会出错。我得到了在我们需要使用的 logback-spring.xml 中包含一个 xml,但不知何故这对我不起作用。我投入了很多时间,但没有结果。 https://dennis-xlc.gitbooks.io/the-logback-manual/content/en/chapter-3-configuration/configuration-file-syntax/file-inclusion.html

logback-common.xml

 <?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
    <property name="LOGS" value="./logs" />
    <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>

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan = "true">
    <property resource ="application.yml"/>
    <springProperty name="NAME" source="spring.application.name" /> 
    <include resource="logback-common.xml"/>
</configuration>

application.yml

spring:
  application:
    name: Logbacking-service

【问题讨论】:

    标签: spring-boot logging logback spring-logback spring-boot-starter


    【解决方案1】:

    必须在 logback-spring.xml 中使用的文件不能包含&lt;xml&gt; 标签。该文件应如下所示。 &lt;included&gt; 标记必须用于必须加载到另一个文件中的文件。

        <included>
                <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>
            
        </included>
    

    【讨论】:

      猜你喜欢
      • 2018-08-06
      • 2021-08-06
      • 1970-01-01
      • 2019-01-20
      • 2014-09-28
      • 1970-01-01
      • 2014-03-08
      • 2013-11-02
      • 2017-09-23
      相关资源
      最近更新 更多