我知道如何通过 environment variables 设置日志级别和 application properties .
有没有办法以编程方式设置它们?
我想为特定测试类(使用 SpringJUnit4ClassRunner 和 @SpringApplicationConfiguration )设置日志级别,但不是全部,并且没有单独的属性每个组合的文件。
我试过defining a non-lazy bean to add a new PropertySource
to the environment ;该方法被调用,但没有效果。
@Bean
@Lazy(false)
public PropertySource testProperties(ConfigurableEnvironment environment) {
PropertySource properties = new MapPropertySource("testProperties", Collections.singletonMap(
"logging.level.org.springframework.security", "DEBUG"
));
environment.getPropertySources().addFirst(properties);
return properties;
}
最佳答案
您可以在测试类上使用@TestPropertySource
。与您尝试的基于 Bean 的方法不同,@TestPropertySource 会在上下文启动之前将属性源添加到环境中,从而允许在初始化日志记录系统时获取属性。
类似这样的事情:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(YourApplication.class)
@TestPropertySource(properties = "logging.level.org.springframework.security:DEBUG")
public class YourApplicationTests {
// …
}
关于java - 如何以编程方式设置 Spring Boot 的日志记录级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35346299/