Disabling Spring log, to have readable logs

前端 未结 5 1212
醉梦人生
醉梦人生 2020-12-05 07:45

How can I disable Spring logs to have log outputs that I can easily read or someone else can read. An answer to a similar question at, how to disable spring bean loading log

相关标签:
5条回答
  • 2020-12-05 08:27

    You can specify the required package name as can be seen in the following example:

    log4j.logger.com.foo=WARN
    

    Now you can see only WARN, ERROR and FATAL logs in console.

    0 讨论(0)
  • 2020-12-05 08:28

    Your default logging, for everything that isn't explictily specified, is DEBUG. So everything is logged at that level (judging from your configuration), basically you are flooding your logs. You should not remove loggers for org.springframework you should add them and set another level for them.

    log4j.logger.org.springframework=INFO 
    

    or whatever log level level you like.

    0 讨论(0)
  • 2020-12-05 08:28

    Before you do, you should have get some knowledge of :

    1.How to add maven dependency 
    2.Where to put log4j configuration file
    

    OK, return to the question.The top answer is not working for spring 4.x, if you are using spring4.x try following 3 steps:

    1. remove common-logging from spring-core

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>4.3.4.RELEASE</version>
          <exclusions>
              <exclusion>
                  <groupId>commons-logging</groupId>
                  <artifactId>commons-logging</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      

      Without this step, no matter what you put in log4j configuration file is not working, cause spring is using common-logging my boy!
      PS:Within lots of spring modules, spring-core is the the only module that explicitly depends on commons-logging.

    2. Add SLF4J and log4j

      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>jcl-over-slf4j</artifactId>
          <version>1.5.8</version>
      </dependency>
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.5.8</version>
      </dependency>
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>1.5.8</version>
      </dependency>
      <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.14</version>
      </dependency>
      
    3. configure log4j.properties(You can also use xml file)

      log4j.rootCategory=INFO, stdout

      log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n

      log4j.category.org.springframework.beans.factory=INFO

    Now, the annoying spring debug log is going away.Enjoy coding!

    The answer is from spring.io doc,for origin click here

    0 讨论(0)
  • 2020-12-05 08:32

    I was also facing this same issue. Springframework logging was not getting removed even after log4j configuration. Then I found that its logging depends on commons-logging.

    You have to disable commons-logging from the dependency in pom.xml file of the web app.

    Even after removing commons-logging from pom.xml please check the dependency hierarchy available in Eclipse or STS IDE. This will help in knowing if somehow its getting added because of some other dependency management which we may not be knowing.

    After removing dependency just add log4j.logger.org.springframework=ERROR to your log4j configuration. This will help.

    0 讨论(0)
  • 2020-12-05 08:38

    All the answers gave examples with configuration in log4j.properties, which is what was asked. I had the same problem but I was using configuration in log4j2.xml and answers here lead me to a solution.

    In case someone is on the same boat as me, here is what I did: I added a node Logger with name org.springframework and level WARN as shown in the following sample:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN">
        <Appenders>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            </Console>
        </Appenders>
        <Loggers>
            <Logger name="org.springframework" level="WARN"/>
            <Root level="info">
                <AppenderRef ref="Console"/>
            </Root>
        </Loggers>
    </Configuration>
    

    I'm using Spring Boot and the exclusion I'm making is logback-classic as shown in the following snippet:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    
    0 讨论(0)
提交回复
热议问题