Menu
Log4j > Log4j XML Configuration

Log4j XML Configuration

In this example we will see how to create more than one appender using xml configuration file. This example is similar to the one we saw before using the properties file (example). So I will simply compare both.

The following code shows the log4j.properties file we used in the previous example.

01.log4j.rootLogger=DEBUG, CA, FA
02. 
03.#Console Appender
04.log4j.appender.CA=org.apache.log4j.ConsoleAppender
05.log4j.appender.CA.layout=org.apache.log4j.PatternLayout
06.log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
07. 
08.#File Appender
09.log4j.appender.FA=org.apache.log4j.FileAppender
10.log4j.appender.FA.File=sample.log
11.log4j.appender.FA.layout=org.apache.log4j.PatternLayout
12.log4j.appender.FA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
13. 
14.# Set the logger level of File Appender to WARN
15.log4j.appender.FA.Threshold = WARN

The following code shows the corresponding log4j.xml file.

01.<?xml version="1.0" encoding="UTF-8" ?>
02.<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
03.<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
04.    <appender name="CA" class="org.apache.log4j.ConsoleAppender">
05.        <layout class="org.apache.log4j.PatternLayout">
06.            <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
07.        </layout>
08.    </appender>
09.    <appender name="FA" class="org.apache.log4j.FileAppender">
10.        <param name="File" value="sample.log"/>
11.        <param name="Threshold" value="WARN"/>
12.        <layout class="org.apache.log4j.PatternLayout">
13.            <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
14.        </layout>
15.    </appender>
16.    <root>
17.        <level value="DEBUG" />
18.        <appender-ref ref="CA" />
19.        <appender-ref ref="FA" />
20.    </root>
21.</log4j:configuration>

You can download the log4j configuration file here.

Source :Download
Source + Lib :Download
Subscribe
Contact Us  |  Copyright 2012 w3javaguide.com