Spring Boot 2.1在调试模式下启动根记录器,直到横幅显示出来?

时间:2018-11-21 20:42:19

标签: java spring spring-boot logging

以下是一些输出,以举例说明我在说什么。调试消息不仅来自Aspect4J记录器,还来自其他几个类。我已经调试了该应用程序,发现Root logger设置为DEBUG,然后在打印Spring Banner后又设置为INFO(我不认为该Banner与它有任何关系)。

我的类路径上没有任何与日志记录相关的文件,除非它们是由我不知道的依赖项添加的。

我也尝试设置。

# Logging
logging.level.root=INFO
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=INFO

在我的application.properties文件中,但这没有帮助,Spring Boot 2.1仍然以DEBUG级别输出所有内容,直到横幅被打印出来为止。关于如何防止Root logger在开始时以DEBUG而不是INFO的任何想法都很好。

15:34:47.971 [main] DEBUG AspectJ Weaver - [AspectJ] not weaving 'org.springframework.boot.logging.AbstractLoggingSystem'
15:34:47.974 [main] DEBUG AspectJ Weaver - [AspectJ] not weaving 'ch.qos.logback.classic.joran.JoranConfigurator'
15:34:47.976 [main] DEBUG AspectJ Weaver - [AspectJ] not weaving 'ch.qos.logback.core.joran.JoranConfiguratorBase'
15:34:47.979 [main] DEBUG AspectJ Weaver - [AspectJ] not weaving 'ch.qos.logback.core.joran.GenericConfigurator'
15:34:47.980 [main] DEBUG AspectJ Weaver - [AspectJ] not weaving 'org.springframework.boot.logging.logback.SpringBootJoranConfigurator'
15:34:47.982 [main] DEBUG AspectJ Weaver - [AspectJ] not weaving 'ch.qos.logback.classic.spi.LoggerContextListener'
15:34:47.985 [main] DEBUG AspectJ Weaver - [AspectJ] not weaving 'ch.qos.logback.classic.turbo.TurboFilter'
15:34:47.987 [main] DEBUG AspectJ Weaver - [AspectJ] not weaving 'org.springframework.boot.logging.logback.LogbackLoggingSystem$1'
15:34:47.988 [main] DEBUG AspectJ Weaver - [AspectJ] not weaving 'org.springframework.boot.logging.LoggerConfigurationComparator'
15:34:47.989 [main] DEBUG AspectJ Weaver - [AspectJ] not weaving 'org.springframework.boot.logging.AbstractLoggingSystem$LogLevels'
15:34:48.009 [main] DEBUG AspectJ Weaver - [AspectJ] not weaving 'org.slf4j.bridge.SLF4JBridgeHandler'

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.1.RELEASE)

2018-11-21 15:34:50.094  INFO 1 --- [           main] c.s.technology.screening.SpringConfig    : Starting SpringConfig on 940e73166080 with PID 1 (/web.jar started by root in /)
2018-11-21 15:34:50.100  INFO 1 --- [           main] c.s.technology.screening.SpringConfig    : No active profile set, falling back to default profiles: default
2018-11-21 15:34:50.420  INFO 1 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4e0ae11f: startup date [Wed Nov 21 15:34:50 EST 2018]; root of context hierarchy
2018-11-21 15:34:53.506  INFO 1 --- [           main] AspectJ Weaver                           : [AspectJ] Join point 'method-execution(org.springframework.data.domain.Page com.sasquatch.technology.screening.api.JobController.listJobs(org.springframework.data.domain.Pageable))' in Type 'com.sasquatch.technology.screening.api.JobController' (JobController.java:48) advised by around advice from 'org.springframework.security.access.intercept.aspectj.aspect.AnnotationSecurityAspect' (AnnotationSecurityAspect.aj:70)
2018-11-21 15:34:53.525  INFO 1 --- [           main] AspectJ Weaver                           : [AspectJ] Join point 'method-execution(com.sasquatch.technology.screening.persistence.entities.Job com.sasquatch.technology.screening.api.JobController.addJob(com.sasquatch.technology.screening.persistence.entities.Job))' in Type 'com.sasquatch.technology.screening.api.JobController' (JobController.java:54) advised by around advice from 'org.springframework.security.access.intercept.aspectj.aspect.AnnotationSecurityAspect' (AnnotationSecurityAspect.aj:70)
2018-11-21 15:34:53.620  INFO 1 --- [           main] AspectJ Weaver                           : [AspectJ] Join point 'method-execution(org.springframework.http.ResponseEntity com.sasquatch.technology.screening.api.TestHelperController.resetDatabase())' in Type 'com.sasquatch.technology.screening.api.TestHelperController' (TestHelperController.java:45) advised by around advice from 'org.springframework.transaction.aspectj.AnnotationTransactionAspect' (AbstractTransactionAspect.aj:66)

2 个答案:

答案 0 :(得分:1)

我能够将问题的根源追溯到一个事实,即Spring在初始化并运行org.springframework.boot.logging.LoggingSystem的实现类之前才设置日志记录级别。当Logback启动时,默认情况下,ch.qos.logback.classic.LoggerContextthis.root.setLevel(Level.DEBUG)中的Root记录器设置为DEBUG。

我将以下代码添加到类内部的静态初始化块中,该块包含引导Spring应用程序的main()方法。获取根记录器(将其初始化为DEBUG),然后转换为Logback记录器。请记住,这意味着此修复程序的实现与类路径上的实际日志记录实现相关。如果我通过log4j或apache commons进行所有日志记录,那么从LoggerFactory.getLogger()获得的记录器将是另一种类型,因此需要不同的转换。由于SLF4J外观不会公开以编程方式更改日志记录级别的方法,因此必须进行强制转换。一旦参考了root记录器,就可以设置级别了。这个级别最终会被Spring覆盖,因此,如果我想自动保持Spring设置和初始化块相同,则需要做更多的工作。

@SpringBootApplication
public class SpringConfig {

    static {
        ((ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).setLevel(Level.INFO);
    }
}

答案 1 :(得分:1)

对于集成或单元测试,另一种可能不太侵入的解决方案是在0 A 1 AB 2 ABC 3 ABCD 4 A 5 AB 6 ABC 7 A 8 AB 9 ABC 10 ABCD 11 ABCDE Name: Variables, dtype: object 中创建logback-test.xml

src/test/resources

来源:https://www.mkyong.com/spring-boot/spring-boot-test-how-to-stop-debug-logs/

这不能完全解决问题中提出的问题,但是搜索引擎在为我的测试寻找解决方案时将我带到了这里,因此我希望其他人也可能会遇到这种情况,这就是为什么我共享此信息。 / p>