Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How do you configure log4j with XML?

This configuration of log4j with XML consists of following files:

a.XMLLog4jExample.java
b.log4j.xml

XMLLog4jExample.java

package com.iqjava.log4j;

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

public class XMLLog4jExample {
static final Logger logger = Logger.getLogger(XMLLog4jExample.class);


public static void main(String[] args) {
DOMConfigurator.configure(
"log4j.xml");
logger.debug(
"This is a debug message");
logger.info(
"This is an info message");
logger.warn(
"This is a warn message");
logger.error(
"This is an error message");
logger.fatal(
"This is a fatal message");

}
}


log4j.xml looks like:







<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="CA" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
</layout>
</appender>
<root>
<level value="debug" />
<appender-ref ref="CA" />
</root>
</log4j:configuration>


Here goes the output of the program:

0 [main] DEBUG com.iqjava.log4j.XMLLog4jExample - This is a debug message
2 [main] INFO com.iqjava.log4j.XMLLog4jExample - This is an info message
2 [main] WARN com.iqjava.log4j.XMLLog4jExample - This is a warn message
2 [main] ERROR com.iqjava.log4j.XMLLog4jExample - This is an error message
2 [main] FATAL com.iqjava.log4j.XMLLog4jExample - This is a fatal message


Eclipse project structure is depicted as in diagram given below:




This post first appeared on Interview Questions On Java,Java EE, please read the originial post: here

Share the post

How do you configure log4j with XML?

×

Subscribe to Interview Questions On Java,java Ee

Get updates delivered right to your inbox!

Thank you for your subscription

×