[英]Spring boot - logging to file using @PropertySource not working
I have a spring boot library module and I want to enable logging into a file.我有一个 spring 引导库模块,我想启用登录文件。 What I did was to add logging settings in module-conf.properties, but no file is created.我所做的是在 module-conf.properties 中添加日志记录设置,但没有创建文件。
module-conf.properties模块配置文件属性
logging.file.name=test.log
I also created a configuration bean:我还创建了一个配置 bean:
@Configuration
@ComponentScan("...")
@EnableJpaRepositories("...")
@PropertySource("classpath:module-conf.properties")
public class ModuleConfiguration {
@Autowired
private Environment environment;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl(environment.getProperty("spring.datasource.url"));
dataSource.setUsername(environment.getProperty("spring.datasource.username"));
dataSource.setPassword(environment.getProperty("spring.datasource.password"));
return dataSource;
}
Do I have to configure the logging system the same way I configure the datasource?我是否必须像配置数据源一样配置日志系统?
Is there a way avoid manual configuration?有没有办法避免手动配置?
This is interesting!这是有趣的!
It appears logging is initialized before the ApplicationContext is created.... and the @PropertySources is read after ApplicationContext is created.看起来日志记录是在创建 ApplicationContext 之前初始化的……并且在创建 ApplicationContext 之后读取@PropertySources。 As per the documentation, you can override the config by updating one of the config file based on your logging system (Spring's default is logback)根据文档,您可以通过根据您的日志系统更新配置文件之一来覆盖配置(Spring 的默认设置是 logback)
Reference: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-custom-log-configuration参考: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-custom-log-configuration
Problem:问题:
Since logging is initialized before the ApplicationContext is created, it is not possible to control logging from @PropertySources in Spring @Configuration files.由于日志记录是在创建 ApplicationContext 之前初始化的,因此无法控制来自 Spring @Configuration 文件中的 @PropertySources 的日志记录。 The only way to change the logging system or disable it entirely is via System properties.更改日志记录系统或完全禁用它的唯一方法是通过系统属性。
The recommendation:建议:
When possible, we recommend that you use the -spring variants for your logging configuration (for example, logback-spring.xml rather than logback.xml).如果可能,我们建议您为日志记录配置使用 -spring 变体(例如,logback-spring.xml 而不是 logback.xml)。 If you use standard configuration locations, Spring cannot completely control log initialization.如果使用标准配置位置,Spring 无法完全控制日志初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.