【问题标题】:logback-spring. is unable to read property value from application.ymllogback-弹簧。无法从 application.yml 读取属性值
【发布时间】:2022-01-22 03:20:06
【问题描述】:

我的 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>

上面是我的 logback-spring.xml。请参考下面我的application.yml:-

spring:
  application:
    name: Logbacking-service

【问题讨论】:

    标签: spring-boot logback spring-logback


    【解决方案1】:

    您可以按照文档here 中的说明在您的 logback 文件中使用以下内容

    <springProperty name = "appname" source= "spring.application.name"/>
    

    然后在其他地方使用它

    <file>${LOGS}/${appname}.log</file>
    

    我测试了您使用的确切代码,它确实存在问题,因此上述解决方案应该也适用于我。之前使用您的代码生成的日志文件名是“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]
    

    【讨论】:

    猜你喜欢
    • 2023-01-30
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2012-05-16
    • 1970-01-01
    • 2015-03-29
    相关资源
    最近更新 更多