As per Apache Log4j FAQ page:
Why do I see a warning about "No appenders found for logger" and "Please configure log4j properly"?
This occurs when the default configuration files log4j.properties
and log4j.xml
can not be found and the application performs no explicit configuration. log4j
uses Thread.getContextClassLoader().getResource()
to locate the default configuration files and does not directly check the file system. Knowing the appropriate location to place log4j.properties or log4j.xml
requires understanding the search strategy of the class loader in use. log4j
does not provide a default configuration since output to the console or to the file system may be prohibited in some environments.
Basically the warning No appenders could be found for logger means that you're using log4j
logging system, but you haven't added any Appenders (such as FileAppender, ConsoleAppender, SocketAppender, SyslogAppender, etc.) into your configuration file or the configuration file is missing.
There are three ways to configure log4j: with a properties file (log4j.properties
), with an XML file and through Java code (rootLogger.addAppender(new NullAppender());
).
log4j.properties
If you've property file present (e.g. when installing Solr), you need to place this file within your classpath directory.
classpath
Here are some command suggestions in Linux how to determine your classpath value:
$ echo $CLASSPATH $ ps wuax | grep -i classpath $ grep -Ri classpath /etc/tomcat? /var/lib/tomcat?/conf /usr/share/tomcat?
or from Java: System.getProperty("java.class.path")
.
Log4j XML
Below is a basic XML configuration file for log4j in XML format:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/> </layout> </appender> <root> <priority value ="debug" /> <appender-ref ref="console" /> </root> </log4j:configuration>
Tomcat
If you're using Tomcat, you may place your log4j.properties
into: /usr/share/tomcat?/lib/
or /var/lib/tomcat?/webapps/*/WEB-INF/lib/
folder.
Solr
For the reference, Solr default log4j.properties
file looks like:
# Logging level solr.log=logs/ log4j.rootLogger=INFO, file, CONSOLE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n #- size rotation with log cleanup. log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.MaxFileSize=4MB log4j.appender.file.MaxBackupIndex=9 #- File to log to and log format log4j.appender.file.File=${solr.log}/solr.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m\n log4j.logger.org.apache.zookeeper=WARN log4j.logger.org.apache.hadoop=WARN # set to INFO to enable infostream log messages log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF
Why can't log4j find my properties file in a J2EE or WAR application?
The short answer: the log4j classes and the properties file are not within the scope of the same classloader.
Log4j only uses the default Class.forName()
mechanism for loading classes. Resources are handled similarly. See the documentation for java.lang.ClassLoader
for more details.
So, if you're having problems, try loading the class or resource yourself. If you can't find it, neither will log4j. ;)
See also: