Lis*_*bon 1 logback spring-boot spring-logback
我的 logback-spring.xml 从 application.properties 读取良好,但从 application.yml 读取不正常。在我的项目中,我们被要求仅使用 YAML 格式,因为同一项目中的其他微服务正在使用此格式,因此我无法添加属性文件。请帮助我为什么我的 application.yml 没有在 logback.xml 中读取
我尝试了多种方法并在 stackoverflow 上搜索了类似的问题,但没有问题有正确的答案**。请不要将此标记为重复**。
<?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 resource ="application.yml"/>
<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}/${spring.application.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>
</configuration>
Run Code Online (Sandbox Code Playgroud)
上面是我的logback-spring.xml。请参考下面我的 application.yml:-
spring:
application:
name: Logbacking-service
Run Code Online (Sandbox Code Playgroud)
您可以按照此处文档中的说明在 logback 文件中使用以下内容
<springProperty name = "appname" source= "spring.application.name"/>
Run Code Online (Sandbox Code Playgroud)
然后在其他地方使用它
<file>${LOGS}/${appname}.log</file>
Run Code Online (Sandbox Code Playgroud)
我测试了您使用的确切代码,它确实出现了问题,因此上述解决方案应该有效,因为它也适用于我。早些时候,您的代码生成的日志文件名是“appname_IS_UNDEFINED.log”,并且发布上述更改后,名称是“Logbacking-service.log”。
如果您在 logback 中为“ org.springframework.core.env.PropertySourcesPropertyResolver”启用跟踪日志记录级别,您将看到 application.yml 从中读取属性的位置。这将帮助您了解 Spring Boot 试图从何处查找配置。像下面这样的东西
Searching for key 'spring.profiles.active' in PropertySource 'applicationConfig: [classpath:/application.yml]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3061 次 |
| 最近记录: |