该教程中,我将向你展现:如何在测试时设置spring boot 日志级别。虽然咱们能够在测试经过时忽略日志,可是若是须要诊断失败的测试,选择正确的日志级别是很是重要的。java
2.日志级别的重要性
正确设置日志级别能够节省咱们许多时间。 举例来讲,若是测试在CI服务器上失败,但在开发服务器上时却经过了。咱们将没法诊断失败的测试,除非有足够的日志输出。 为了获取正确数量的详细信息,咱们能够微调应用程序的日志级别,若是发现某个java包对咱们的测试更加剧要,能够给它一个更低的日志级别,好比DEBUG。相似地,为了不日志中有太多干扰,咱们能够为那些不过重要的包配置更高级别的日志级别,例如INFO或者ERROR。 一块儿来探索设置日志级别的各类方法吧!git
3. application.properties中的日志设置
若是想要修改测试中的日志级别,咱们能够在src/test/resources/application.properties
设置属性:github
logging.level.com.baeldung.testloglevel=DEBUG
该属性将会为指定的包com.baeldung.testloglevel
设置日志级别。 一样地,咱们能够经过设置root
日志等级,更改全部包的日志级别web
logging.level.root=INFO
如今经过添加REST端点写入日志,来尝试下日志设置。spring
@RestController public class TestLogLevelController { private static final Logger LOG = LoggerFactory.getLogger(TestLogLevelController.class); @Autowired private OtherComponent otherComponent; @GetMapping("/testLogLevel") public String testLogLevel() { LOG.trace("This is a TRACE log"); LOG.debug("This is a DEBUG log"); LOG.info("This is an INFO log"); LOG.error("This is an ERROR log"); otherComponent.processData(); return "Added some log output to console..."; } }
正如所料,若是咱们在测试中调用这个端点,咱们将能够看到来自TestLogLevelController
的调试日志。shell
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log 2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log 2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
这样设置日志级别十分简单,若是测试用@SpringBootTest
注解,那么咱们确定应该这样作。可是,若是不使用该注解,则必须以另外一种方式配置日志级别。服务器
3.1 基于Profile的日志设置
尽管将配置放在src\test\application.properties
在大多数场景下好用,但在某些状况下,咱们可能但愿为一个或一组测试设置不一样的配置。 在这种状况下,咱们可使用@ActiveProfiles
注解向测试添加一个Spring Profile
:app
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class) @EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class) @ActiveProfiles("logging-test") public class TestLogLevelWithProfileIntegrationTest { // ... }
日志设置将会存在src/test/resources
目录下的application-logging-test.properties
中:spring-boot
logging.level.com.baeldung.testloglevel=TRACE logging.level.root=ERROR
若是使用描述的设置调用TestLogLevelCcontroller
,将看到controller中打印的TRACE
级别日志,而且不会看到其余包出现INFO级别以上的日志。学习
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log 2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
4.配置Logback
若是使用Spring Boot默认的Logback
,能够在src/test/resources
目录下的logback-text.xml
文件中设置日志级别:
<configuration> <include resource="/org/springframework/boot/logging/logback/base.xml"/> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n </pattern> </encoder> </appender> <root level="error"> <appender-ref ref="STDOUT"/> </root> <logger name="com.baeldung.testloglevel" level="debug"/> </configuration>
以上例子如何在测试中为Logback配置日志级别。 root日志级别设置为INFO,com.baeldung.testloglevel
包的日志级别设置为DEBUG。 再来一次,看看提交以上配置后的日志输出状况
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log 2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log 2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
4.1 基于Profile配置Logback
另外一种配置指定Profile文件的方式就是在application.properties
文件中设置logging.config
属性:
logging.config=classpath:logback-testloglevel.xml
或者,若是想在classpath只有一个的Logback配置,能够在logbacl.xml
使用springProfile
属性。
<configuration> <include resource="/org/springframework/boot/logging/logback/base.xml"/> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n </pattern> </encoder> </appender> <root level="error"> <appender-ref ref="STDOUT"/> </root> <springProfile name="logback-test1"> <logger name="com.baeldung.testloglevel" level="info"/> </springProfile> <springProfile name="logback-test2"> <logger name="com.baeldung.testloglevel" level="trace"/> </springProfile> </configuration>
如今使用logback-test1
配置文件调用TestLogLevelController
,将会得到以下输出:
2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log 2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
另外一方面,若是更改配置为logback-test2
,输出将变成以下:
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log 2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log 2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
5.可选的Log4J
另外,若是咱们使用Log4J2
,咱们能够在src\main\resources
目录下的log4j2-spring.xml
文件中配置日志等级。
<Configuration> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" /> </Console> </Appenders> <Loggers> <Logger name="com.baeldung.testloglevel" level="debug" /> <Root level="info"> <AppenderRef ref="Console" /> </Root> </Loggers> </Configuration>
咱们能够经过application.properties
中的logging.config
属性来设置Log4J 配置的路径。
logging.config=classpath:log4j-testloglevel.xml
最后,查看使用以上配置后的输出:
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is a DEBUG log 2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an INFO log 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.testloglevel.TestLogLevelController : This is an ERROR log 2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an INFO log from another package 2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.b.component.OtherComponent : This is an ERROR log from another package
6.结论
在本文中,咱们学习了如何在Spring Boot测试应用程序时设置日志级别,并探索了许多不一样的配置方法。在Spring Boot
应用程序中使用application.properties
设置日志级别是最简便的,尤为是当咱们使用@SpringBootTest注解时。 与往常同样,这些示例的源代码都在GitHub上。
做者:baeldung
译者:Leesen
