【发布时间】:2016-05-22 15:22:13
【问题描述】:
我知道如何通过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;
}
【问题讨论】:
-
记录器启动后,我知道配置日志级别的唯一方法是通过 JMX...
-
@BoristheSpider 仅适用于
java.util.logging,spring-boot 不使用。 -
在这两个方面都不是真的——Logback 和 Log4j2 都支持 JMX,如果你想要的话,Spring Boot 支持 JUL(但你为什么要......)。您只需要在配置文件中配置 JMX 连接器。你没有说你把 Boot 插入了哪个框架。
-
宾果游戏。虽然我希望有一种方法可以在上下文启动之前更改属性。
-
@BoristheSpider 我确实说过,但通过标签。
标签: java spring-boot slf4j logback