Disabling Spring log, to have readable logs

2019-01-14 02:55发布

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 suggested to comment out all lines having org.springframework substring in log4j.properties file. In my case there no such lines.

Here is log4j.properties

# Define the root logger with appender file
log4j.rootLogger = DEBUG, stdout

# Define the file appender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# Set the name of the logs destination
log4j.appender.stdout.target=System.out

# Set the immediate flush to true (default)
log4j.appender.stdout.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.stdout.Threshold=debug

# Set the append to false, overwrite
log4j.appender.stdout.Append=false

# Define the layout for appender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.conversionPattern=%d{yyyy-MM-dd}:%m%n 

5条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-14 03:25

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>
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-14 03:30

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.

查看更多
Fickle 薄情
4楼-- · 2019-01-14 03:36

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

查看更多
欢心
5楼-- · 2019-01-14 03:36

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.

查看更多
We Are One
6楼-- · 2019-01-14 03:51

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.

查看更多
登录 后发表回答