【发布时间】:2019-03-15 06:41:19
【问题描述】:
我正在尝试在我的 Springboot 应用程序中使用 logback-spring.xml 创建日志记录功能,但无法读取 logback-spring 中的属性值(例如:log.dest.path)。 xml 文件。
我尝试过的方法:
我正在通过@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 方法来加载property 文件,因为property 文件被外部放置在其中一个环境中。
当应用程序启动时,不会在属性文件中提供的指定路径中创建日志文件,而是在项目根目录中创建一个带有 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。
【问题讨论】:
-
共享应用程序属性文件和项目浏览器
-
共享文件内容。logback-spring.xml、appliaction.dev.yml在resource文件夹下
-
我能够通过链接-github.com/spring-projects/spring-boot/issues/… 让它工作。现在我使用 Springprofilesin logback-spring.xml 来获得更大的灵活性
标签: java spring spring-boot logback spring-profiles