【问题标题】:Spring Boot 2.1 starts the root logger in debug mode until the banner prints?Spring Boot 2.1 以调试模式启动根记录器,直到横幅打印?
【发布时间】:2019-04-24 12:02:01
【问题描述】:

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

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

我也试过设置。

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

在我的 application.properties 文件中,但这没有帮助,Spring Boot 2.1 仍然在 DEBUG 级别输出所有内容,直到横幅打印。关于如何防止 Root 记录器在开始时出现 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)

【问题讨论】:

    标签: java spring spring-boot logging


    【解决方案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 外观不公开以编程方式更改日志记录级别的方法。一旦我参考了根记录器,我就设置了级别。这个级别最终会被 Spring 覆盖,所以如果我想自动保持 Spring 设置和我的初始化块相同,我需要做更多的工作。

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

    【讨论】:

      【解决方案2】:

      对于集成或单元测试,另一种侵入性可能较小的解决方案是在src/test/resources 中创建logback-test.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <configuration>
          <include resource="org/springframework/boot/logging/logback/base.xml" />
          <logger name="org.springframework" level="INFO"/>
      </configuration>
      

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

      这并没有完全解决问题中提出的问题,但是搜索引擎在为我的测试搜索解决方案时将我带到了这里,所以我希望它可能会发生在其他人身上,这就是我分享此信息的原因。

      【讨论】:

      • 您不应该在生产代码中使用 logback-test.xml。这是在单元/集成测试期间覆盖的。如果你想使用 logback 文件(也许 logback 在 spring 配置它之前配置它自己),那么你可以使用 logback-spring.xml 或 logback.xml。我认为这个问题可以通过自定义 logback.xml 来解决。 docs.spring.io/spring-boot/docs/current/reference/html/…
      • @Jazzepi 如果您像我一样在运行单元测试时登陆此页面寻找解决同样问题的方法,那么logback-test.xml 很好。此外,它被命名为 -test 并存储在 src/test/resources 中(注意路径中的 test),因此很明显这不是用于生产的!我认为你的反对票是不必要的。
      • 如果您想将您的答案专门用于集成测试,我很乐意删除标记。但是,如果您只是在谈论测试,那么答案并没有真正回答所提出的问题,我必须标记为删除,因为它没有回答关于在所有环境中控制输出的问题。
      • @Jazzepi 查看我的编辑。如果您想到其他要说的内容,也可以随意编辑它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 2014-09-22
      • 2023-01-31
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多