Thursday, May 31, 2012

Log4j using XML configuration.


Please follow the following program to understand log4j implementation using xml file.
Here I have used console appender to log the events.

Java : XMLConfigurationDemo
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class XMLConfigurationDemo {

      private static final Logger logger = Logger.getLogger(XMLConfigurationDemo.class);

      public static void main(String[] args) {
            DOMConfigurator.configure("log.xml");
            logger.trace("Trace Message.");// Point to Point.
            logger.debug("Debug Message.");// Method to Method.
      }
}
XML:log.xml
<xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
      <appender name="Console" class="org.apache.log4j.ConsoleAppender">
            <layout class="org.apache.log4j.PatternLayout">
                  <param name="ConversionPattern" value="%p %c - %m%n" />
            </layout>
      </appender>
      <root>
            <level value="trace" />
            <appender-ref ref="Console" />
      </root>
</log4j:configuration>

No comments:

Post a Comment