如何设置Spring Boot测试时的日志级别

作者:锅外的大佬 时间:2023-11-10 14:11:20 

1.概览

该教程中,我将向你展示:如何在测试时设置spring boot 日志级别。虽然我们可以在测试通过时忽略日志,但是如果需要诊断失败的测试,选择正确的日志级别是非常重要的。

2.日志级别的重要性

正确设置日志级别可以节省我们许多时间。

举例来说,如果测试在CI服务器上失败,但在开发服务器上时却通过了。我们将无法诊断失败的测试,除非有足够的日志输出。

为了获取正确数量的详细信息,我们可以微调应用程序的日志级别,如果发现某个java包对我们的测试更加重要,可以给它一个更低的日志级别,比如DEBUG。类似地,为了避免日志中有太多干扰,我们可以为那些不太重要的包配置更高级别的日志级别,例如INFO或者ERROR。

一起来探索设置日志级别的各种方法吧!

3. application.properties中的日志设置

如果想要修改测试中的日志级别,我们可以在src/test/resources/application.properties设置属性:


logging.level.com.baeldung.testloglevel=DEBUG

该属性将会为指定的包com.baeldung.testloglevel设置日志级别。

同样地,我们可以通过设置root日志等级,更改所有包的日志级别


logging.level.root=INFO

现在通过添加REST端点写入日志,来尝试下日志设置。


@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的调试日志。


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:


@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中:


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上。

来源:https://juejin.im/post/5cf8c2be51882576be2760dd

标签:springboot,测试,日志级别
0
投稿

猜你喜欢

  • 聊一聊jdk1.8中的ArrayList 底层数组是如何扩容的

    2023-11-16 08:55:50
  • Android 虚拟按键与沉浸式的适配方法

    2021-11-27 22:03:02
  • Android详解之NoHttp最基本使用(无封装)

    2022-09-28 17:11:51
  • C#中DataTable 转换为 Json的方法汇总(三种方法)

    2021-12-12 16:41:00
  • string boot 与 自定义interceptor的实例讲解

    2023-10-27 17:03:20
  • Spring注解Autowired的底层实现原理详解

    2022-10-19 11:49:44
  • Java Annotation注解相关原理代码总结

    2023-11-18 02:13:57
  • java实现员工工资管理系统

    2023-09-22 15:37:24
  • Springboot整合Redis最简单例子分享

    2021-12-18 17:53:48
  • C字符串操作函数的实现详细解析

    2022-10-26 16:28:37
  • Android中系统自带锁WalkLock与KeyguardLock用法实例详解

    2023-11-26 01:50:49
  • 解决Android ListView数据为空及加载错误的方法

    2022-11-30 06:57:41
  • C#自定义画刷原理解析

    2021-06-19 19:17:32
  • 使用Prometheus+Grafana的方法监控Springboot应用教程详解

    2023-10-31 13:08:12
  • @valid 无法触发BindingResult的解决

    2023-08-10 09:16:12
  • Google Kaptcha 框架实现登录验证码功能(SSM 和 SpringBoot)

    2023-01-14 09:21:38
  • Java DOM4J方式生成XML的方法

    2022-07-19 02:32:42
  • Java Class 加密工具 ClassFinal详解

    2023-02-10 14:58:48
  • Java判断ip是否为IPV4或IPV6地址的多种方式

    2023-03-28 01:18:31
  • Java MultipartFile实现上传文件/上传图片

    2022-04-19 07:07:19
  • asp之家 软件编程 m.aspxhome.com