java log4j 警告问题 - apache commons
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9991471/
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
log4j warning issue - apache commons
提问by PapaSmurf
I'm using apache commons library and log4j. I have an xml configuration file and a log4j.properties files. I want to specify my log4j properties path inside my xml configuration file. To load my settings i do:
我正在使用 apache 公共库和 log4j。我有一个 xml 配置文件和一个 log4j.properties 文件。我想在我的 xml 配置文件中指定我的 log4j 属性路径。要加载我的设置,我会这样做:
//Loading my xml file
this.config = new XMLConfiguration(this.xmlFileName);
At this moment the following warnings are raised:
此时发出以下警告:
log4j:WARN No appenders could be found for logger (org.apache.commons.configuration.ConfigurationUtils).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
However i haven't yet called any log4j object. Once i have read the xml file i can successfully work with my log4j instance. Is there any way to remove those warnings?
但是我还没有调用任何 log4j 对象。一旦我阅读了 xml 文件,我就可以成功地使用我的 log4j 实例。有什么办法可以消除这些警告吗?
采纳答案by ChrLipp
Log4J outputs this error when a logger is created, but no appender(s) is(are) defined. In practice this error occurs when a logger is created before log4j is initialized.
Log4J 在创建记录器时输出此错误,但未定义任何附加程序。实际上,在 log4j 初始化之前创建记录器时会发生此错误。
You say you haven't called any log4j object. But in the error message you see that org.apache.commons.configuration.ConfigurationUtils
creates a logger object (see line 66).
你说你没有调用任何 log4j 对象。但是在错误消息中,您会看到org.apache.commons.configuration.ConfigurationUtils
创建了一个记录器对象(请参阅第66行)。
You could turn it off before initialization, see How to turn off log4j warnings?
您可以在初始化之前将其关闭,请参阅如何关闭 log4j 警告?
Logger.getRootLogger().setLevel(Level.OFF);
There should be no need to turn it on again since the initialization sets normally the log level of the root logger.
应该不需要再次打开它,因为初始化通常会设置根记录器的日志级别。
回答by Michael Fernando
Check if the log4j.properties file is in the classpath
检查 log4j.properties 文件是否在类路径中
This link might be useful: http://www.coderanch.com/t/63230/open-source/log-log-WARN-No-appenders
此链接可能有用:http: //www.coderanch.com/t/63230/open-source/log-log-WARN-No-appenders
回答by PapaSmurf
I resolve my issue with this workaround:
我用这个解决方法解决了我的问题:
//Disable log level
Logger.getRootLogger().setLevel(Level.OFF);
Now i can read my xml configuration file without WARNINGS.
After set log level:
现在我可以在没有警告的情况下读取我的 xml 配置文件。
设置日志级别后:
Logger.getRootLogger().setLevel(Level.INFO);
回答by Ken Chan
You should at least set the appender and the logger level for the root logger in the loaded log4j configuration file. Otherwise , you will see this warning message.
您至少应该在加载的 log4j 配置文件中为根记录器设置附加程序和记录器级别。否则,您将看到此警告消息。
Example of setting the appender and logger level for the root logger:
为根记录器设置附加程序和记录器级别的示例:
#Set root logger 's level and its appender to an appender called CONSOLE which is defined below.
log4j.rootLogger=DEBUG, CONSOLE
#Set the behavior of the CONSOLE appender
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%t] %-5p %c - %m%n