Thursday, May 31, 2012

Log4j with multiple appenders using xml file.


Please follow the following program to understand log4j implementation for multiple appenders using xml file.
Java : MultipleAppenders
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class MultipleAppenders {

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

      public static void main(String[] args) {
            DOMConfigurator.configure("log.xml");
            logger.debug("Debug Message.");// Method to Method.
            logger.info("Info Message.");// Information passing and recieving.
            logger.warn("Warning Message.");// Retrying again.
      }
}
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>
      <appender name="Html" class="org.apache.log4j.FileAppender">
            <param name="File" value="sample.html"/>       
            <layout class="org.apache.log4j.HTMLLayout"/>                    
      </appender>
      <appender name="File" class="org.apache.log4j. FileAppender ">
            <param name="File" value="sample.log"/>
            <layout class="org.apache.log4j.PatternLayout">                   
                  <param name="ConversionPattern" value="%p %c - %m%n" />           
            </layout>        
      </appender>
      <root>
            <level value="trace" />
            <appender-ref ref="Console" />
            <appender-ref ref="File" />
            <appender-ref ref="Html" />
      </root>
</log4j:configuration>

No comments:

Post a Comment