Wednesday, July 31, 2013

Log4j with XML file

                          For executing Log4j programs,we are using properties file for configuring details of Log4j. In this post, we will configure Log4j details with XML file.And we can keep this file under src folder. 

log4j.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="myAppender" class="org.apache.log4j.ConsoleAppender">
<layout   class="org.apache.log4j.SimpleLayout"/>
</appender>
<root>
<priority     value = "debug"     />
<appender-ref ref   = "myAppender"/>
</root>

</log4j:configuration>

Log4JJava.java :

package com.log4j;

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


public class Log4JJava{ 

static Logger log = Logger.getLogger(Log4JJava.class.getName());

public static void main(String[] args) { 
                /* By using DOMConfigurator.configure() method and passing xml file as argument */
DOMConfigurator.configure("log4j.xml");
               /* call debug() method on log object and passing String message as argument */
log.debug("Debug");
log.info("Info"); 


}

No comments:

Post a Comment