Spring Boot Test ignores logging.level

自古美人都是妖i 提交于 2019-11-28 17:21:43

Okay what I did now, in all modules I configured as follows:

src/main/resources:
I use logging configuration in application.properies like logging.level.* as descrbed in question.

src/test/resources:
I use logback-test.xml like:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="*.myapp" level="error" />
    <logger name="org.springframework.core " level="error" />
    <logger name="org.springframework.beans" level="error" />
    <logger name="org.springframework.context" level="error" />
    <logger name="org.springframework.transaction" level="error" />
    <logger name="org.springframework.web" level="error" />
    <logger name="org.springframework.test" level="error" />
    <logger name="org.hibernate" level="error" />
</configuration>

But I still don't understand, why in few modules I could use application.properties, but in another module it ignores ... But for now it works for me as it is.

But maybe few hints with background knowledge are still welcome.

I dont mark my answer as solution, cos it still feels like a workaround.

To enable application.properties need to add an annotation @SpringBootTest to test class, read more here

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <logger name="org.springframework" level="INFO"/>
</configuration>

As a fast fix, I put logback.xml file with the above content in src/test/resources and it works.

I'm also looking for a solution to that, meanwhile I'm using the following solution:

this isn't the best but it works

@BeforeClass
public static void setErrorLogging() {
   LoggingSystem.get(ClassLoader.getSystemClassLoader()).setLogLevel(Logger.ROOT_LOGGER_NAME, LogLevel.ERROR);
}

LoggingSystem: a common abstraction over logging systems.

->

get: Detect and return the logging system in use. Supports Logback and Java Logging

setLogLevel: Sets the logging level for a given logger.

Make sure to change back log level for all other test classes.

Hope it helps you, goodluck

If your tests are annotated with @DataJpaTest you can switch Hibernate SQL logging off with:

@DataJpaTest(showSql=false)
public class MyTest {
  ..
}

Try this:

@ContextConfiguration(classes = ApplicationImportBackend.class, 
    initializers = ConfigFileApplicationContextInitializer.class)
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({ "test" })
public class BatchJobConfigurationTests {
    //...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!