【问题标题】:How to include a custom xml in logback-spring.xml?如何在 logback-spring.xml 中包含自定义 xml?
【发布时间】:2022-01-22 16:43:36
【问题描述】:

我创建了一个通用日志back-common.xml。我想在另一个文件-logback.spring.xml 中使用这个文件。请帮助我如何有效地使用它。

截至目前,应用程序正在启动,但日志未在控制台中打印,并且日志未填充到日志文件中。请帮忙。不要将其标记为重复,因为我已经尝试了几乎所有东西,并且我已经为此投入了 2 天。与此相关的其他问题没有附加有效答案。

logback-spring.xml

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

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>
    
    <!-- LOG everything at INFO level -->
    <root level="info">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </root>

    <!-- LOG "com.baeldung*" at TRACE level -->
    <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>

application.yml

spring:
  application:
    name: Logbacking-service

【问题讨论】:

标签: spring-boot logging logback spring-logback


【解决方案1】:

您需要在下面为您的子配置使用“资源”而不是“文件”(因为它存在于类路径中)

<include resource="logback-common.xml"/>

当 logback 在初始阶段启动时,当 logback 能够找到并加载所有配置文件时,您会在控制台上看到打印的信息。

更新:-关于您分享的错误-您还需要在子配置文件中使用included 标签而不是configuration 标签。测试了以下配置,它对我来说工作正常。

logback-spring.xml

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

和子配置一样

logback-common.xml

<?xml version="1.0" encoding="UTF-8"?>
<included>
    <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>
    
    <!-- LOG everything at INFO level -->
    <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>

【讨论】:

  • Spring Boot 在类路径中搜索替代文件位置(参见documentation),因此logback-spring.xml 是正确的。
  • @Piotr 感谢您指出这一点。我已经编辑了答案。
  • 谢谢你 Shailendra :) 感激不尽。在将其更改为资源时,我收到此错误---- 日志系统无法使用来自“null”的配置进行初始化 java.lang.IllegalStateException:检测到 Logback 配置错误:ch.qos.logback.core.joran.spi 中的错误。 Interpreter@2:16 - [configuration] 没有适用的操作,当前 ElementPath 是 [[configuration][configuration]] ch.qos.logback.core.joran.spi.Interpreter@3:77 中的错误 - [ 没有适用的操作include],当前 ElementPath 是 [[configuration][configuration][include]]
  • @Lisbon - 关于错误,请参阅答案中的更新。
  • 非常感谢先生 :) 现在明白了
猜你喜欢
  • 2019-01-19
  • 1970-01-01
  • 2018-11-07
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
  • 2017-03-18
  • 2011-03-04
  • 2017-11-17
相关资源
最近更新 更多