Basic steps to configure Log4j using xml and properties file

July 14, 2008

Log4J

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.

Books: If you are looking to buy a good book for reading the Log4j for your project development, we would recommend the Pro Apache Log4j and The Complete Log4j Manual. Also 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

log4j.properties

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.
log4j.appender.Appender1=org.apache.log4j.
                                ConsoleAppender
log4j.appender.Appender2=org.apache.log4j.
                              RollingFileAppender
log4j.appender.Appender2.File=sample.log

# Appender2 uses PatternLayout.
log4j.appender.Appender1.layout=org.apache.log4j.
                                       PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=
                               %-4r [%t] %-5p %c %x - %m%n

log4j.appender.Appender2.layout=org.apache.log4j.
                                        PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=
                                 %-4r [%t] %-5p %c %x - %m%n

log4j.xml

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

xmlns:log4j=
              "http://jakarta.apache.org/log4j/"
               debug="false">

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

   <appender name="fileAppender"
           class="org.apache.log4j.RollingFileAppender">
      <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" />
      </layout>
   </appender>

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

</log4j:configuration>

Log4jPropertyTest.java

package javabeat.net.log4j;

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

/**
 * source : www.javabeat.net
 */
public class Log4jPropertyTest {
    private static Logger logger = Logger.getLogger
               (Log4jPropertyTest.class);
    public static void main (String args[]){
        PropertyConfigurator.configure("log4j.properties");
        logger.info("Test Log");
    }
}

Log4jXmlTest.java

package javabeat.net.log4j;

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

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

}
email

Comments

comments

  • Anu

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

    • suthukrish

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

      Thanks,
      Krishna

    • Krish

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

  • Shweta

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

    • Krish

      Yes. You can specify the file name there.

      • kava

        how to generate log files in separate folders..i am unable to see any log message in console also..

        • Krish

          Have you added fileAppender?

  • Guest

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

    • suthu_krish

       Fixed the problem

      Thanks,
      Krishna

  • Pingback: JavaPins

  • deeps

    when i tried to implement logger using xml file got this
    “java.io.filenot 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

  • Guest

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

  • Pingback: Externalize Log4j | MadBit.org