禁用 Spring 日志,以获得可读的日志

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18648014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:12:56  来源:igfitidea点击:

Disabling Spring log, to have readable logs

springlogginglog4jstdout

提问by LeandreM

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 logsuggested to comment out all lines having org.springframeworksubstringin log4j.propertiesfile. In my case there no such lines.

如何禁用 Spring 日志以获得我可以轻松阅读或其他人可以阅读的日志输出。对类似问题的回答,如何禁用 spring bean 加载日志建议注释掉所有包含org.springframeworksubstringin 的行log4j.propertiesfile。就我而言,没有这样的线路。

Here is log4j.properties

这是 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 

回答by M. Deinum

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.

对于未明确指定的所有内容,您的默认日志记录是DEBUG。所以一切都记录在那个级别(从你的配置来看),基本上你正在淹没你的日志。您不应该删除 org.springframework 的记录器,您应该添加它们并为它们设置另一个级别。

log4j.logger.org.springframework=INFO 

or whatever log level level you like.

或您喜欢的任何日志级别。

回答by Ajax

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:

好,回到问题。最佳答案不适用于 spring 4.x,如果您使用的是 spring4.x,请尝试执行以下 3 个步骤:

  1. remove common-loggingfrom 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-loggingmy boy!
    PS:Within lots of spring modules, spring-coreis 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

  1. 除去common-loggingspring-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>
    

    没有这一步,不管你在log4j配置文件里放什么都行不通,因为spring正在使用common-logging我的孩子!
    PS:在许多 spring 模块中,spring-core是唯一明确依赖commons-logging.

  2. 添加 SLF4J 和 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. 配置log4j.properties(也可以使用xml文件)

    log4j.rootCategory=信息,标准输出

    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=信息

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

现在,烦人的 spring 调试日志消失了。享受编码吧!

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

答案来自 spring.io doc,来源点击这里

回答by Vlad G.

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.

所有的答案都给出了在 log4j.properties 中配置的示例,这就是被问到的。我有同样的问题,但我在 log4j2.xml 中使用配置,这里的答案引导我找到解决方案。

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:

如果有人和我在同一条船上,这就是我所做的:我添加了一个名为 org.springframework 和级别为 WARN 的节点记录器,如下面的示例所示:

<?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:

我正在使用 Spring Boot 并且我所做的排除是 logback-classic,如以下代码段所示:

    <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>

回答by Hemanth

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.

现在您只能在控制台中看到 WARN、ERROR 和 FATAL 日志。

回答by SachinSarawgi

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.

我也面临同样的问题。即使在配置 log4j 之后,Springframework 日志记录也没有被删除。然后我发现它的日志记录依赖于commons-logging。

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

您必须禁用commons-loggingweb 应用程序的 pom.xml 文件中的依赖项。

Even after removing commons-loggingfrom 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.

即使commons-logging从 pom.xml 中删除后,请检查 Eclipse 或 STS IDE 中可用的依赖关系层次结构。这将有助于了解它是否因为一些我们可能不知道的其他依赖管理而被添加。

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

删除依赖项后,只需添加log4j.logger.org.springframework=ERROR到您的 log4j 配置中。这会有所帮助。