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

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");
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!