【问题标题】:Spring Boot Logback logging DEBUG messagesSpring Boot Logback 记录 DEBUG 消息
【发布时间】:2015-07-10 17:35:19
【问题描述】:

我在 Spring Boot 将 DEBUG 级别的日志项吐出到终端时遇到了问题,它应该处于 INFO 级别。

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </layout>
    </appender>

    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>

    <logger name="org.springframework.web" level="WARN"/>

</configuration>

pom.xml 中的依赖项 (省略了与日志记录无关的所有内容)

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Logging -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
    </dependency>
</dependencies>

终端中不断出现的行

20:01:00.937 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'serverServletmapping' in [servletConfigInitParams]
20:01:00.937 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'serverServletmapping' in [servletContextInitParams]
20:01:00.937 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'serverServletmapping' in [systemProperties]
20:01:00.937 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'serverServletmapping' in [systemEnvironment]
20:01:00.937 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'serverServletmapping' in [random]
20:01:00.937 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'serverServletmapping' in [applicationConfig: [classpath:/application.properties]]
20:01:00.938 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'serverServletmapping' in [class path resource [sql.properties]]
20:01:00.938 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'serverServletmapping' in [localProperties]
20:01:00.938 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'serverServletmapping' in any property source. Returning [null]
20:01:00.938 [main] DEBUG o.s.core.env.MutablePropertySources - Adding [servletConfigInitParams] PropertySource with lowest search precedence
20:01:00.938 [main] DEBUG o.s.core.env.MutablePropertySources - Adding [servletContextInitParams] PropertySource with lowest search precedence
20:01:00.938 [main] DEBUG o.s.core.env.MutablePropertySources - Adding [systemProperties] PropertySource with lowest search precedence
20:01:00.938 [main] DEBUG o.s.core.env.MutablePropertySources - Adding [systemEnvironment] PropertySource with lowest search precedence
20:01:00.938 [main] DEBUG o.s.core.env.MutablePropertySources - Adding [random] PropertySource with lowest search precedence
20:01:00.938 [main] DEBUG o.s.core.env.MutablePropertySources - Adding [applicationConfig: [classpath:/application.properties]] PropertySource with lowest search precedence
20:01:00.938 [main] DEBUG o.s.core.env.MutablePropertySources - Adding [class path resource [sql.properties]] PropertySource with lowest search precedence

【问题讨论】:

  • 你在application.properties或application.yml中有日志配置吗?
  • @AndyWilkinson Wilkinson 不,application.properties 完全为空,application.yml 不存在。

标签: java spring maven spring-boot logback


【解决方案1】:

看起来 logback 没有找到您的 logback.xml 文件。请参考logback documentation on config file location

logback.groovylogback-test.xmllogback.xml 等配置文件可以直接位于类路径中声明的任何文件夹下。例如,如果类路径读取c:/java/jdk15/lib/rt.jar;c:/mylibs/,那么logback.xml文件应该直接位于c:/mylibs/下,即c:/mylibs/logback.xml

将其放在c:/mylibs/ 的子文件夹下,例如c:/mylibs/other/,将不起作用。

对于网络应用,配置文件可以直接放在WEB-INF/classes/下

【讨论】:

  • 确实如此,我可以控制所有其他记录器的日志记录级别,并且我在 ch.qos.logback.classic.LoggerContext[default] 中得到一行`00:17:50,675 |-INFO - 找到日志中 [file:/.../src/main/resources/logback.xml]` 的资源 [logback.xml]。
【解决方案2】:
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>ERROR</level>
    </filter>
    <encoder>
        <pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
        </pattern>
    </encoder>
</appender>

添加此过滤器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 2021-03-15
    • 1970-01-01
    • 2018-09-17
    • 2021-08-27
    相关资源
    最近更新 更多