如何在 logback-spring.xml 中包含自定义 xml?
How to include a custom xml in logback-spring.xml?
我已经创建了一个公共日志备份-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
您需要使用下面的“资源”而不是“文件”来为您的子配置(因为它存在于类路径中)
<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>
我已经创建了一个公共日志备份-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
您需要使用下面的“资源”而不是“文件”来为您的子配置(因为它存在于类路径中)
<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>