英文:
Spring boot - logging to file using @PropertySource not working
问题
我有一个 Spring Boot 库模块,我想要将日志记录到文件中。
我所做的是在 module-conf.properties 文件中添加日志记录设置,但是没有创建任何文件。
module-conf.properties
logging.file.name=test.log
我还创建了一个配置 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;
}
}
我需要像配置数据源一样配置日志系统吗?
是否有一种方式可以避免手动配置?
英文:
I have a spring boot library module and I want to enable logging into a file.
What I did was to add logging settings in module-conf.properties, but no file is created.
module-conf.properties
logging.file.name=test.log
I also created a configuration 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?
答案1
得分: 3
这很有趣!
看起来日志记录在创建ApplicationContext之前就被初始化了... 而 @PropertySources 是在创建ApplicationContext之后被读取的。根据文档,您可以通过更新一个基于您的日志系统的配置文件来覆盖配置(Spring的默认日志系统是logback)
问题:
> 由于日志记录是在创建ApplicationContext之前初始化的,因此不可能通过Spring的@Configuration文件中的 @PropertySources 来控制日志记录。改变日志记录系统或完全禁用它的唯一方法是通过系统属性。
建议:
> 当有可能时,我们建议您使用带有 -spring 后缀的变体进行日志配置(例如,使用 logback-spring.xml 而不是 logback.xml)。如果使用标准配置位置,Spring 无法完全控制日志初始化。
英文:
This is interesting!
It appears logging is initialized before the ApplicationContext is created.... and the @PropertySources is read after ApplicationContext is created. 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)
Problem:
> Since logging is initialized before the ApplicationContext is created,
> it is not possible to control logging from @PropertySources in Spring
> @Configuration files. 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). If you use standard configuration locations, Spring
> cannot completely control log initialization.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论