如何抑制Spring配置INFO消息?

时间:2017-12-06 13:23:04

标签: java spring maven logback slf4j

我最近通过STS IDE开始使用Spring。在运行独立应用程序时,我一直试图摆脱所有INFO日志消息。我在这里和其他地方看过类似的问题,这些问题帮助我减少了INFO信息的数量,但没有完全压制它们

我现在已经创建了一个最小的Spring / Maven遗留应用程序(即。不使用Spring Boot)测试应用程序:

public class Main
{
    public static void main (String[] args)
    {
        try(ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationContext.class))
        {
            System.out.println("Hello");
        }
    }
}

使用空的应用程序上下文:

@Configuration
public class ApplicationContext
{
}

我已将POM降至最低:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.samples</groupId>
  <artifactId>TestLog</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>

        <!-- Generic properties -->
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Spring -->
        <spring-framework.version>3.2.3.RELEASE</spring-framework.version>

        <!-- Logging -->
        <logback.version>1.0.13</logback.version>
        <slf4j.version>1.7.5</slf4j.version>

    </properties>

    <dependencies>
        <!-- Spring and Transactions -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- Logging with SLF4J & LogBack -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies> 
</project>

根据STS POM编辑器,会产生以下已解决的依赖项列表:

  • aopalliance
  • 共享记录
  • logback-classic(运行时)
  • logback-core(运行时)
  • SLF4J-API
  • 弹簧的AOP
  • 弹簧豆
  • 弹簧上下文
  • 弹簧芯
  • 弹簧表达

根据此处列出的类似问题的答案,我已经包含以下commons-logging.properties文件:

org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
org.apache.commons.logging.simplelog.defaultlog=ERROR

以及以下logback.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
    </encoder>
  </appender>

   <logger name="org.springframework" level="ERROR"/>

  <root level="ERROR">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

使用日志记录配置文件,运行Java应用程序将生成以下输出:

[INFO] AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Wed Dec 06 13:00:17 UTC 2017]; root of context hierarchy
[INFO] DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@649d209a: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,applicationContext,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Hello
[INFO] AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Wed Dec 06 13:00:17 UTC 2017]; root of context hierarchy
[INFO] DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@649d209a: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,applicationContext,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy

[INFO]行来自何处​​以及如何抑制它们?

0 个答案:

没有答案