如何设置SpringBoot测试时的⽇志级别
1.概览
该教程中,我将向你展⽰:如何在测试时设置spring boot ⽇志级别。虽然我们可以在测试通过时忽略⽇志,但是如果需要诊断失败的测试,选择正确的⽇志级别是⾮常重要的。
2.⽇志级别的重要性
正确设置⽇志级别可以节省我们许多时间。
举例来说,如果测试在CI服务器上失败,但在开发服务器上时却通过了。我们将⽆法诊断失败的测试,除⾮有⾜够的⽇志输出。
为了获取正确数量的详细信息,我们可以微调应⽤程序的⽇志级别,如果发现某个java包对我们的测试更加重要,可以给它⼀个更低的⽇志级别,⽐如DEBUG。类似地,为了避免⽇志中有太多⼲扰,我们可以为那些不太重要的包配置更⾼级别的⽇志级别,例如INFO或者ERROR。
⼀起来探索设置⽇志级别的各种⽅法吧!
3. application.properties中的⽇志设置
如果想要修改测试中的⽇志级别,我们可以在src/test/resources/application.properties设置属性:
logging.stloglevel=DEBUG
该属性将会为指定的包stloglevel设置⽇志级别。
同样地,我们可以通过设置root⽇志等级,更改所有包的⽇志级别
=INFO
现在通过添加REST端点写⼊⽇志,来尝试下⽇志设置。
@RestController
public class TestLogLevelController {
private static final Logger LOG = Logger(TestLogLevelController.class);
@Autowired
private OtherComponent otherComponent;
@GetMapping("/testLogLevel")
public String testLogLevel() {
LOG.debug("This is a DEBUG log");
LOG.info("This is an INFO log");
<("This is an ERROR log");
otherComponent.processData();
return "Added some log output ";
}
}
正如所料,如果我们在测试中调⽤这个端点,我们将可以看到来⾃TestLogLevelController的调试⽇志。
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is a DEBUG log
2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is an INFO log
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is an ERROR log
2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.bponent.OtherComponent : This is an INFO log from another package
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.bponent.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.stloglevel=TRACE
=ERROR
如果使⽤描述的设置调⽤TestLogLevelCcontroller,将看到controller中打印的TRACE级别⽇志,并且不会看到其他包出现INFO级别以上的⽇志。
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is a DEBUG log
2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is an INFO log
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is an ERROR log
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.bponent.OtherComponent : This is an ERROR log from another package
4.配置Logback
如果使⽤Spring Boot默认的Logback,可以在src/test/resources⽬录下的l⽂件中设置⽇志级别:
<configuration>
<include resource="/org/springframework/boot/logging/l"/>
<appender name="STDOUT" class="ch.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="stloglevel" level="debug"/>
</configuration>
以上例⼦如何在测试中为Logback配置⽇志级别。
root⽇志级别设置为INFO,stloglevel包的⽇志级别设置为DEBUG。
再来⼀次,看看提交以上配置后的⽇志输出情况
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is a DEBUG log
2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is an INFO log
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is an ERROR log
2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.bponent.OtherComponent : This is an INFO log from another package
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.bponent.OtherComponent : This is an ERROR log from another package
4.1 基于Profile配置Logback
另⼀种配置指定Profile⽂件的⽅式就是在application.properties⽂件中设置fig属性:
log4j2不打印日志或者,如果想在classpath只有⼀个的Logback配置,可以在l使⽤springProfile属性。
<configuration>
<include resource="/org/springframework/boot/logging/l"/>
<appender name="STDOUT" class="ch.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="stloglevel" level="info"/>
</springProfile>
<springProfile name="logback-test2">
<logger name="stloglevel" level="trace"/>
</springProfile>
</configuration>
现在使⽤logback-test1配置⽂件调⽤TestLogLevelController,将会获得如下输出:
2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is an INFO log
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is an ERROR log
2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.bponent.OtherComponent : This is an INFO log from another package
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.bponent.OtherComponent : This is an ERROR log from another package
另⼀⽅⾯,如果更改配置为logback-test2,输出将变成如下:
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is a DEBUG log
2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is an INFO log
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is an ERROR log
2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.bponent.OtherComponent : This is an INFO log from another package
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.bponent.OtherComponent : This is an ERROR log from another package
5.可选的Log4J
另外,如果我们使⽤Log4J2,我们可以在src\main\resources⽬录下的l⽂件中配置⽇志等级。
<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="stloglevel" level="debug" />
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
我们可以通过application.properties中的fig属性来设置Log4J 配置的路径。
最后,查看使⽤以上配置后的输出:
2019-04-01 14:08:27.545 DEBUG 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is a DEBUG log
2019-04-01 14:08:27.545 INFO 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is an INFO log
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] stloglevel.TestLogLevelController : This is an ERROR log
2019-04-01 14:08:27.546 INFO 56585 --- [nio-8080-exec-1] c.bponent.OtherComponent : This is an INFO log from another package
2019-04-01 14:08:27.546 ERROR 56585 --- [nio-8080-exec-1] c.bponent.OtherComponent : This is an ERROR log from another package
6.结论
在本⽂中,我们学习了如何在Spring Boot测试应⽤程序时设置⽇志级别,并探索了许多不同的配置⽅法。在Spring Boot应⽤程序中使⽤application.properties设置⽇志级别是最简便的,尤其是当我们使⽤@SpringBootTest注解时。
与往常⼀样,这些⽰例的源代码都在上。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论