简体   繁体   中英

log4j warning issue - apache commons

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:

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

Check if the log4j.properties file is in the classpath

This link might be useful: http://www.coderanch.com/t/63230/open-source/log-log-WARN-No-appenders

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.

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

You could turn it off before initialization, see How to turn off log4j warnings?

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.

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:

Logger.getRootLogger().setLevel(Level.INFO);

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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