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

原文链接:www.baeldung.com/spring-boot…

作者:baeldung

译者:Leesen

Spring Boot 测试时的日志级别的更多相关文章

  1. Spring Boot log4j多环境日志级别的控制

    之前介绍了在<Spring boot中使用log4j>,仅通过log4j.properties对日志级别进行控制,对于需要多环境部署的环境不是很方便,可能我们在开发环境大部分模块需要采用D ...

  2. 【Kafka】Kafka测试时控制台日志级别修改

    在学习Kafka客户端时日志打的飞起,根本看不到自己发的消息,找了半天网上竟然没有这方面的资料.想了下依赖关系,这里应该只要把slf4j的日志级别调整应该就ok了. static { LoggerCo ...

  3. Spring Boot系列一:默认日志logback配置解析

    前言 今天来介绍下Spring Boot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢 如何引入日志? 日志输出格式以及输出方式如何配置? 代码中如何使用? 正文 Sp ...

  4. Spring Boot Logback几种日志详解

    日志对于应用程序来说是非常重要的,Spring框架本身集成了不少其他工具,我们自身的应用也会使用到第三方库,所以我们推荐在Spring应用中使用SLF4J/Logback来记录日志. SLF4J与Lo ...

  5. Spring Boot中使用logback日志框架

    说明:Spring Boot在最新的版本中默认使用了logback框架.一般来说使用时只需在classpath下创建logback.xml即可,而官方推荐使用logback-spring.xml替代, ...

  6. Spring boot使用log4j打印日志

    先将maven中spring-boot-starter的日志spring-boot-starter-logging去掉 <dependency> <groupId>org.sp ...

  7. Spring Boot 学习摘要--关于日志框架

    date: 2020-01-05 16:20:00 updated: 2020-01-08 15:50:00 Spring Boot 学习摘要--关于日志框架 学习教程来自:B站 尚硅谷 1. 关于日 ...

  8. spring boot使用slf4j输出日志

    spring boot使用slf4j输出日志 https://blog.csdn.net/qq442270636/article/details/79406346 Spring Boot SLF4J日 ...

  9. 使用Spring boot整合Hive,在启动Spring boot项目时,报错

    使用Spring boot整合Hive,在启动Spring boot项目时,报出异常: java.lang.NoSuchMethodError: org.eclipse.jetty.servlet.S ...

随机推荐

  1. HDU——1397Goldbach's Conjecture(二分查找+素数打表)

    Goldbach's Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  2. BZOJ1227 [SDOI2009]虔诚的墓主人 【树状数组】

    题目 小W 是一片新造公墓的管理人.公墓可以看成一块N×M 的矩形,矩形的每个格点,要么种着一棵常青树,要么是一块还没有归属的墓地.当地的居民都是非常虔诚的基督徒,他们愿意提前为自己找一块合适墓地.为 ...

  3. Linux目录结构简析

    Linux目录结构简析 Linux继承了unix操作系统结构清晰的特点.在linux下的文件结构非常有条理.但是,上述的优点只有在对linux相当熟悉时,才能体会到.现在,虫虫就把linux下的目录结 ...

  4. socket编程-微软小兵

    socket两端建立连接,不断开的连接的情况下做数据交互,客户端发送数据和服务端返回数据.直到客户端要求断开,则关闭连接. 代码目录结构:

  5. A* k短路 学习笔记

    题目大意 n个点,m条边有向图,给定S,T,求不严格k短路 n<=1000 m<=100000 k<=1000 不用LL 分析 A*算法 f(i)表示从S出发经过i到T的估价函数 \ ...

  6. 我要好offer之 搜索算法大总结

    1. 二分搜索 详见笔者博文:二分搜索的那些事儿,非常全面 2. 矩阵二分搜索 (1) 矩阵每行递增,且下一行第一个元素大于上一个最后一个元素 (2) 矩阵每行递增,且每列也递增 3. DFS 深度优 ...

  7. <编程精粹:编写高质量C语言代码> 读书笔记

    0.规则<The Elements of Programming Style><The Elements of Style> 1.假想的编译程序(1)使用编译器提供的所有的可选 ...

  8. es6总结(七)--proxy & reflect

  9. 转 PHP中exec、system等函数调用linux命令问题

    PHP中exec.system等函数调用linux命令问题 先小说两句:今天研究了下PHP调用LINUX命令的功能,一开始怎么做都调用不成功,试了好久才终于成功了,所以发出来分享一下.下面我将详细介绍 ...

  10. Java 自定义序列化、反序列化

    1.如果某个成员变量是敏感信息,不希望序列化到文件/网络节点中,比如说银行密码,或者该成员变量所属的类是不可序列化的, 可以用 transient 关键字修饰此成员变量,序列化时会忽略此成员变量. c ...