簡體   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