Over a million developers have joined DZone.

Multiple Appenders Using XML File


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.

log4j.rootLogger=DEBUG, CA, FA

#Console Appender
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

#File Appender
log4j.appender.FA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Set the logger level of File Appender to WARN
log4j.appender.FA.Threshold = WARN

The following code shows the corresponding log4j.xml file.

<?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" />
	<appender name="FA" class="org.apache.log4j.FileAppender">
		<param name="File" value="sample.log"/>
		<param name="Threshold" value="WARN"/>
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
		<level value="DEBUG" />
		<appender-ref ref="CA" />
		<appender-ref ref="FA" />

You can download the log4j configuration file here.

Source: Download

Source + Lib: Download



{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}