Basic steps to configure Log4j using xml and properties file

This example demonstrated how to configure Log4j setup using the Properties file and XML file. These are the two most widely used techniques for configuring the Log4j for your application. But, in the recent days configuring with properties files are considered to be old technique and recommended using XML.

This example program uses simple standalone java program for running the example. But, in most of the project scenarios it will be used in the web application. However the configuration file will be the same.

You would consider reading the list of Java books recommended.

If you are Java developer and would like to receive the daily updates on Java tips or techniques, please subscribe here.

Configure Log4j

It is the traditional properties file for configuring the log4j mechanism. It is not used by most of the projects because of the XML configuration’s flexibility.

# Set root logger level to DEBUG and its only appender
              to Appender1.
log4j.rootLogger=INFO, Appender1,Appender2

# Appender1 is set to be a ConsoleAppender.

# Appender2 uses PatternLayout.
                               %-4r [%t] %-5p %c %x - %m%n

                                 %-4r [%t] %-5p %c %x - %m%n


It is mostly used and recommended configuration for setting up the log4j mechanism.

<?xml version="1.0" encoding="UTF-8"?>
<!--<span class="hiddenSpellError" pre=""-->DOCTYPE log4j:configuration SYSTEM "log4j.dtd">


   <appender name="consoleAppender"
      <param name="Threshold" value="INFO" />
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d
             %-5p  [%c{1}] %m %n" />

   <appender name="fileAppender"
      <param name="Threshold" value="INFO" />
      <param name="File" value="sample.log"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d
                %-5p  [%c{1}] %m %n" />

   <logger name="" additivity="false" >
      <level value="INFO" />
      <appender-ref ref="consoleAppender"/>
      <appender-ref ref="fileAppender"/>



import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

 * source :
public class Log4jPropertyTest {
    private static Logger logger = Logger.getLogger
    public static void main (String args[]){
        PropertyConfigurator.configure("");"Test Log");


import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

 * source :
public class Log4jXmlTest {
    private static Logger logger = Logger.getLogger
    public static void main (String args[]){
        DOMConfigurator.configure("log4j.xml");"Test Log");




About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.


  1. Best article on Log4j! Saved my time! Thank you.

    • suthukrish says:

      Thanks you for the comments!! If you have any issues on configuring the log4j, please post it here.


    • Thank you for reading the articles. Please post it here If you have any suggestions.

  2. Shweta says:

    I want my log file to get generated in a generalised unix server location, how can i specify the location in log4j.xml? Can the file name be specified as under??
    <param name="File" value="${root}Application/logs/Myservice.log"/>

  3. Thanks for the article, but i think there is a little mistake in the title ;)

  4. when i tried to implement logger using xml file got this
    “ found exception : E:iRapidiRapideclipselog4j.xml (The system cannot find the file specified)”
    Cant we give path of log4j.xml file of our project instead of storing it to eclipse folder

  5. Provide some explanation instead of just pasting some code. difficult to understand for a beginner in Log4j

  6. Nice article.
    one question if my available on classpath can I remove following line

  7. gurpreet bajwa says:

    I wanted to know how the log4j jar reads the data in the log4j.xml? Where can I find that particular piece of code?

  8. I followed instructions as mentioned but getting the following exception:

    Exception in thread “main” java.lang.ExceptionInInitializerError
    Caused by: java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext

    Plz help

    • Eduardo says:

      Me too

      • Hey Eduardo
        I got this exception because I didnt downloaded the xact jar file
        it should be “log4j-1.2.17.jar”
        try importing this file
        and include “import org.apache.log4j.Logger;”.
        worked in my case you should also try :)


  1. JavaPins says:

    Basic steps to configure Log4j using xml and properties file…

    Thank you for submitting this cool story – Trackback from JavaPins…

Speak Your Mind