Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Boot 2.1在调试模式下启动根记录器,直到标题打印出来?_Java_Spring_Spring Boot_Logging - Fatal编程技术网

Java Spring Boot 2.1在调试模式下启动根记录器,直到标题打印出来?

Java Spring Boot 2.1在调试模式下启动根记录器,直到标题打印出来?,java,spring,spring-boot,logging,Java,Spring,Spring Boot,Logging,下面是一些输出,以举例说明我所说的内容。调试消息不仅仅来自Aspect4J记录器,它们还来自其他几个类。我调试了应用程序,发现根记录器设置为DEBUG,然后在打印Spring横幅后设置为INFO(我不认为该横幅与此有关) 我的类路径上没有任何与日志相关的文件,除非它们是由我不知道的依赖项添加的 我也试过设置 # Logging logging.level.root=INFO logging.level.org.springframework.web=INFO logging.level.org.

下面是一些输出,以举例说明我所说的内容。调试消息不仅仅来自Aspect4J记录器,它们还来自其他几个类。我调试了应用程序,发现根记录器设置为DEBUG,然后在打印Spring横幅后设置为INFO(我不认为该横幅与此有关)

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

我也试过设置

# Logging
logging.level.root=INFO
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=INFO
在我的application.properties文件中,SpringBoot2.1仍然以调试级别输出所有内容,直到标题打印出来。任何关于如何防止根记录器在开始时作为调试而不是信息出现的想法都是非常好的

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)

我能够将问题的根源追溯到Spring在初始化并运行org.springframework.boot.logging.LoggingSystem的实现类之前没有设置日志级别这一事实。当Logback启动时,它会在
ch.qos.Logback.classic.LoggerContext
this.Root.setLevel(Level.DEBUG)
中默认设置根记录器进行调试

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

@SpringBootApplication
public class SpringConfig {

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

对于集成或单元测试,另一种可能不那么麻烦的解决方案是在
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>

资料来源:


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

您不应该在生产代码中使用logback-test.xml。用于在单元/集成测试期间重写。如果您想使用logback文件(可能logback在spring配置它之前配置了自己),那么您可以使用logback-spring.xml或logback.xml。我认为这个问题可以通过定制logback.xml来解决@Jazzepi如果像我一样,在运行单元测试时,您在本页上搜索相同问题的解决方案,那么
logbacktest.xml
就可以了。另外,它被命名为
-test
,并存储在
src/test/resources
(注意路径中的测试),因此很明显这不是用于生产的!我认为你投反对票是不必要的。如果你想把你的答案说成是关于集成测试的,我很乐意去掉这个分数。但是,如果您只是谈论测试,那么答案并不是真正回答所问的问题,我必须标记删除,因为它没有回答关于在所有环境中控制输出的问题。@Jazzepi请参阅我的编辑。如果你想说别的话,也可以随意编辑。