如何在没有@springboottest 的情况下设置日志级别?
How set log level without @springboottest?
我决定写一个没有“@SpringBootTest”的"component"测试。遇到 application.yml 中的某些设置不起作用的问题。
我尝试更改日志记录级别 - 不起作用。我做错了什么?
TestdemoApplicationTests
@RunWith(SpringRunner.class)
@EnableConfigurationProperties
@ContextConfiguration(classes = TestConf.class, initializers = TestContextInitializer.class)
public class TestdemoApplicationTests {
@Test
public void logLevel() {
}
}
测试会议
@Configuration
public class TestConf {
}
TestContextInitializer
public class TestContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
try {
Resource resource = applicationContext.getResource("classpath:application.yml");
YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
PropertySource<?> yamlTestProperties = sourceLoader.load("applicationProperties", resource, null);
applicationContext.getEnvironment().getPropertySources().addFirst(yamlTestProperties);
String[] profiles = applicationContext.getEnvironment().getProperty("spring.profiles.active").replaceAll(" ", "").split(",");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
application.yml
spring:
profiles:
active: junit
logging:
level:
ROOT: INFO
springframework.web: INFO
hibernate: INFO
Look at my Screenshot (Expected log level is info, but actual is debug
P.S。我知道我的问题可能微不足道,但在我提出之前,我在 Google 和 Stack 上查看了很多信息。
P.S.S 不要使用@SpringBootTest
Spring 测试不受应用程序级日志记录设置的约束。
您应该在 ../test/resources/
下放置一个单独的 logback 配置文件
在XML中:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="org.springframework" level="INFO"/>
</configuration>
顺便说一句,还需要在您的 yml 中指定完整包,例如:
logging:
level:
org.springframework.web: INFO
我决定写一个没有“@SpringBootTest”的"component"测试。遇到 application.yml 中的某些设置不起作用的问题。
我尝试更改日志记录级别 - 不起作用。我做错了什么?
TestdemoApplicationTests
@RunWith(SpringRunner.class)
@EnableConfigurationProperties
@ContextConfiguration(classes = TestConf.class, initializers = TestContextInitializer.class)
public class TestdemoApplicationTests {
@Test
public void logLevel() {
}
}
测试会议
@Configuration
public class TestConf {
}
TestContextInitializer
public class TestContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
try {
Resource resource = applicationContext.getResource("classpath:application.yml");
YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
PropertySource<?> yamlTestProperties = sourceLoader.load("applicationProperties", resource, null);
applicationContext.getEnvironment().getPropertySources().addFirst(yamlTestProperties);
String[] profiles = applicationContext.getEnvironment().getProperty("spring.profiles.active").replaceAll(" ", "").split(",");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
application.yml
spring:
profiles:
active: junit
logging:
level:
ROOT: INFO
springframework.web: INFO
hibernate: INFO
Look at my Screenshot (Expected log level is info, but actual is debug
P.S。我知道我的问题可能微不足道,但在我提出之前,我在 Google 和 Stack 上查看了很多信息。 P.S.S 不要使用@SpringBootTest
Spring 测试不受应用程序级日志记录设置的约束。 您应该在 ../test/resources/
下放置一个单独的 logback 配置文件在XML中:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="org.springframework" level="INFO"/>
</configuration>
顺便说一句,还需要在您的 yml 中指定完整包,例如:
logging:
level:
org.springframework.web: INFO