How to initialize log4j properly? – Dev

The best answers to the question “How to initialize log4j properly?” in the category Dev.


After adding log4j to my application I get the following output every time I execute my application:

log4j:WARN No appenders could be found for logger (slideselector.facedata.FaceDataParser).
log4j:WARN Please initialize the log4j system properly.

It seems this means a configuration file is missing.
Where should this config file be located and what is a good start content?

I’m using plain java for developing a desktop application. So no webserver etc…


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.


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


# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n


# Keep one backup file

log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n


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:

$ 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"/> 

    <priority value ="debug" /> 
    <appender-ref ref="console" /> 


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
log4j.rootLogger=INFO, file, CONSOLE


log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n

#- size rotation with log cleanup.

#- File to log to and log format
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:

  • Short introduction to log4j at Apache site
  • Apache: Logging Services: FAQ at Apache site


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

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