简体   繁体   English

Spring 引导 - 使用 @PropertySource 记录到文件不起作用

[英]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.

相关问题 Spring 当 Spring PropertySource 外部文件与外部 Tomcat 时,引导日志记录不起作用 - Spring Boot Logging not working when Spring PropertySource External File with External Tomcat 在春季启动中使用@PropertySource读取属性文件时出错 - Error while reading property file using @PropertySource in spring boot Spring Boot:如何使用@PropertySource指向jar外部的文本文件? - Spring Boot : how to use @PropertySource to point to a text file outside the jar? Spring Boot + Yaml + @PropertySource + @ConfigurationProperties + 属性源文件中的列表未注入 - Spring Boot + Yaml + @PropertySource + @ConfigurationProperties + List in Property Source file not injecting Spring 引导记录到文件 - Spring Boot Logging to a File Spring Boot 使用 PropertySource 为每个环境加载配置 - Spring boot Load configuration per environment using PropertySource Spring Boot:更改PropertySource的顺序 - Spring Boot: Change the order of the PropertySource 在Spring Boot中使用@TestPropertySource覆盖@PropertySource - Override @PropertySource with @TestPropertySource in Spring Boot 在Spring Boot(Gradle)中无法记录日志 - Logging not working in Spring Boot (Gradle) 使用Spring PropertySource无法通过绝对路径找到属性文件 - Cannot find a properties file by absolute path using Spring PropertySource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM