简体   繁体   English

禁用log4j记录到控制台

[英]Disable log4j logging to console

I have the following log4j.properties file: 我有以下log4j.properties文件:

# Direct log messages to a log file
log4j.appender.fileerror=org.apache.log4j.RollingFileAppender
log4j.appender.fileerror.File=C\:\\logs_eba\\loging-error.log
log4j.appender.fileerror.MaxFileSize=20MB
log4j.appender.fileerror.MaxBackupIndex=1
log4j.appender.fileerror.layout=org.apache.log4j.PatternLayout
log4j.appender.fileerror.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 
log4j.appender.fileerror.threshold = WARN

# Direct log messages to stdout
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.out
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Root logger option
log4j.rootLogger = WARN, fileerror

But my application keeps directing logs to my Console, it even shows logs at info level even though I defined WARN threshold. 但是我的应用程序始终将日志定向到我的控制台,即使我定义了WARN阈值,它甚至仍以信息级别显示日志。

For reference, this is how I get my Logging objects: 作为参考,这是获取日志对象的方式:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

    private static final Logger log = LoggerFactory.getLogger(MyClass.class.getSimpleName());
log.warn("My log message");

Can it be related to my use of slf4j? 可能与我对slf4j的使用有关吗?

PS. PS。 I am using Spring + Hibernate as my architecture, and their logs are also being redirected to console. 我使用Spring + Hibernate作为我的体系结构,它们的日志也被重定向到控制台。

Log4J by default looks up for log4j.properties file in the classpath and will consider the one found first. 默认情况下,Log4J在类路径中查找log4j.properties文件,并将考虑首先找到的文件。 So if your application is depending on a library which itself is dependent on log4j and also provides log4j.properties of its own, then there is a chance that it is getting selected. 因此,如果您的应用程序依赖于本身依赖于log4j并且还提供了自己的log4j.properties的库,那么就有可能被选中。

So I would recommend you to specify custom location for your properties using 因此,我建议您使用以下方法为属性指定自定义位置

java -Dlog4j.configuration=customName

Or use -Dlog4j.debug to find helpful info about the log4j configuration. 或使用-Dlog4j.debug查找有关log4j配置的有用信息。

As mentioned in the answer by Narendra, this might be because one of your dependent libraries has a log4j.properties of its own, which is being picked up by the application (Your log4j.properties is being ignored). 正如Narendra在回答中所提到的,这可能是因为您的一个依赖库中有一个自己的log4j.properties,它已由应用程序拾取(您的log4j.properties被忽略)。 Ideally, this should not happen as most open source libraries do not include a log4j.properties file. 理想情况下,这应该不会发生,因为大多数开放源代码库都不包含log4j.properties文件。 They expect the calling module to provide one. 他们希望调用模块提供一个。

Find out which module this properties file is coming from. 找出此属性文件来自哪个模块。 If it is an internal project that you have control over, you should remove the log4j.properties from it. 如果它是您可以控制的内部项目,则应从其中删除log4j.properties。 If this is an external jar, try overriding your log4j.properties file over it. 如果这是一个外部jar,请尝试覆盖它的log4j.properties文件。 How you do this will depend on how you are deploying your jars together eg using maven, file copy etc. 如何执行将取决于您如何一起部署jars,例如使用maven,文件复制等。

PS: IMHO, no standard open source project should include its own log4j.properties file for this exact reason. PS:恕我直言,出于这个确切原因,没有标准的开源项目应包括其自己的log4j.properties文件。 If you are using such a library, may be its not very mature and you might want to have a look at alternatives. 如果您使用的是这样的库,则它可能不是很成熟,您可能想看看替代方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM