繁体   English   中英

在Springboot中无法读取logback-spring.xml中的属性

[英]Unable to read properties in logback-spring.xml in Springboot

我正在尝试在Springboot应用程序中使用logback-spring.xml创建日志记录功能,但无法读取logback-spring.xml文件中的属性值(例如:log.dest.path)。

我尝试的方法:

我通过@PropertySource基于配置文件动态加载用于不同环境(dev,stage,prod)的属性文件(YAML)。 分析工作正常,并且已加载正确的YAML文件(例如:-application.dev.yml)

logback-spring.xml:

    <configuration debug="true">
  <property name="PROFILE" value="-${spring.profiles.active}" />
  <timestamp key="timestamp" datePattern="yyyy-MM-dd" />
 <springProperty scope="context" name="destination"
        source="log.dest.path" />
    <springProperty name="fileName" scope="context"
        source="spring.application.name" />
    <springProfile name="dev">
        <appender name="FILE" class="ch.qos.logback.core.FileAppender">
            <file>${destination}/log${PROFILE}/${fileName}_${PROFILE}-${timestamp}.log</file>
            <encoder>
                <pattern>%date %level [%thread] %logger{10} [%file : %line] %msg%n</pattern>
            </encoder>
        </appender>
    </springProfile>
    <springProfile name="production">
        <appender name="FILE" class="ch.qos.logback.core.FileAppender">
            <file>${destination}/log${PROFILE}/${fileName}_${PROFILE}-${timestamp}.log</file>
            <encoder>
                <pattern>%date %level [%thread] %logger{10} [%file : %line] %msg%n</pattern>
            </encoder>
        </appender>
    </springProfile>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%msg%n</pattern>
        </encoder>
    </appender>

    <logger name="com.test" additivity="true">
        <appender-ref ref="FILE" />
    </logger>

    <root level="debug">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

配置文件:

@Configuration
public class Config {

    @Configuration
    @Profile({ "dev", "default" })
    @PropertySource(factory = YamlPropertySourceFactory.class, value = { "classpath:application.dev.yml" })
    static class DevConfig {
    }

    @Configuration
    @Profile("stage")
    @PropertySource(factory = YamlPropertySourceFactory.class, value = { "file:////D://file//config.yml" })
    static class StageConfig {
    }
}

我使用@PropertySource方法加载属性文件,因为属性文件从外部放置在一种环境中。

当应用程序启动时,没有在属性文件中提供的指定路径中创建日志文件,而是在项目根目录中创建了一个带有destination_IS_UNDEFINED的文件夹。

但是我通过以下代码获取环境属性:

@SpringBootApplication
public class ProfileProject extends SpringBootServletInitializer {

    public static void main(String[] args) {
        ConfigurableApplicationContext configurableApplicationContext = new SpringApplicationBuilder(
                ProfileProject.class).sources(ProfileProject.class).run(args);
        ConfigurableEnvironment environment = configurableApplicationContext.getEnvironment();      
        System.out.println("Env variables Log Dest path ----> : " + environment.getProperty("log.dest.path"));

    }

}

application.dev.yml: -示例

server:
  port: 7999
app:
  name: DEVELOPMENT
spring:
  application:
    name: ProfileDEV
log:
  dest:
    path: D:\development
logging:
 config: classpath:logback-spring.xml

工作方法:

具有所有配置文件级别的属性到各自的yaml文件(例如:-application。{env} .yml),并具有application.properties文件,其中放置了logback-spring.xml所需的属性。

注意:-

SpringBoot-logging,不等待@ProperySource完成配置。 结果,通过此方法加载的属性文件将不会被logback-spring.xml读取,但会读取application.properties。

您可以将scope属性与上下文值一起使用以从环境访问该属性。

您还可以设置defaultValue属性。

例如:

<springProperty scope="context" name="destination" 
                source="log.dest.path" defaultValue="\temp"/>

如果未加载logback-spring.xml,则在application.properties文件中添加一个属性。

logging.config: classpath:./logback-spring.xml

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM