How to initialize log4j properly?

ID : 10167

viewed : 42

Tags : log4jlog4j

Top 5 Answer for How to initialize log4j properly?

vote vote


Log4j by default looks for a file called or log4j.xml on the classpath.

You can control which file it uses to initialize itself by setting system properties as described here (Look for the "Default Initialization Procedure" section).

For example:

java -Dlog4j.configuration=customName .... 

Will cause log4j to look for a file called customName on the classpath.

If you are having problems I find it helpful to turn on the log4j.debug:


It will print to System.out lots of helpful information about which file it used to initialize itself, which loggers / appenders got configured and how etc.

The configuration file can be a java properties file or an xml file. Here is a sample of the properties file format taken from the log4j intro documentation page:

log4j.rootLogger=debug, stdout, R  log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n  log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=example.log  log4j.appender.R.MaxFileSize=100KB # Keep one backup file log4j.appender.R.MaxBackupIndex=1  log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 
vote vote


While setting up log4j properly is great for "real" projects you might want a quick-and-dirty solution, e.g. if you're just testing a new library.

If so a call to the static method


will setup basic logging to the console, and the error messages will be gone.

vote vote


If you just get rid of everything (e.g. if you are in tests)

org.apache.log4j.BasicConfigurator.configure(new NullAppender()); 
vote vote


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 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 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 (, with an XML file and through Java code (rootLogger.addAppender(new NullAppender());).

If you've property file present (e.g. when installing Solr), you need to place this file within your classpath directory.


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


If you're using Tomcat, you may place your into: /usr/share/tomcat?/lib/ or /var/lib/tomcat?/webapps/*/WEB-INF/lib/ folder.


For the reference, Solr default 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  # set to INFO to enable infostream log messages 

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:

vote vote


You can set the location of your from inside your java app by using:


More information is available here:

Top 3 video Explaining How to initialize log4j properly?