log4j.xml example configurations

Filed Under: Java

We can configure log4j using both property file as well as xml file. Today we will look into log4j.xml example and get the details of log4j.xml configuration.


Here is a typical log4j.xml example file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"

<!-- console appender -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
	<param name="Target" value="System.out" />
	<layout class="org.apache.log4j.PatternLayout">
		<param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
	<filter class="org.apache.log4j.varia.LevelMatchFilter">
		<param name="LevelToMatch" value="INFO" />
        <param name="AcceptOnMatch" value="true" />
	<filter class="org.apache.log4j.varia.DenyAllFilter"/>

<logger name="com.journaldev.log4j" additivity="false">
	<level value="DEBUG" />
	<appender-ref ref="console" />

	<priority value="DEBUG" />
	<appender-ref ref="console" />


If you are using Eclipse, you will notice that it’s not showing you XML element options, this is because it’s not able to find the log4j.dtd file. Something like below image.
log4j.xml example eclipse no default proposals

Change the DOCTYPE declaration to below to fix this. You would need internet connection to validate the log4j.xml file for this.

<!DOCTYPE log4j:configuration PUBLIC
  "-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">

log4j xml example with doctype log4j.dtd fix

log4j.xml example configuration attributes

Let’s look at the log4j.xml example configuration attributes – threshold and debug.

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" threshold="info" debug="false">

The “threshold” attribute takes the minimum level below which all logging statements are disabled. This overrides all logger level configurations, use this only when you want to disable some lower level logging for all the loggers. In above case, all the trace and debug logs will be disabled even if any of the logger level is defined as debug.

The “debug” attribute is used to switch the printing of internal log4j logging statements. You should keep it false to avoid bulk logging by log4j framework. If you will change it to true, you will get some logs like below.

log4j: reset attribute= "false".
log4j: Threshold ="warn".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [com.journaldev.log4j] additivity to [false].
log4j: Level value for com.journaldev.log4j is  [TRACE].
log4j: com.journaldev.log4j level set to TRACE
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Setting property [target] to [System.out].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%-5p %c{1} - %m%n].
log4j: Adding appender named [console] to category [com.journaldev.log4j].
log4j: Level value for root is  [DEBUG].
log4j: root level set to DEBUG
log4j: Adding appender named [console] to category [root].

log4j.xml example – appender

The next important part is appender element. Here we define logging strategy, we can have multiple appenders defined in log4j.xml configuration.

Every appender has a distinct name and class to be used for logging. Then we set the parameters used by the appender class, every class has it’s own set of parameter that can be defined.

For example, “Target” is parameter in ConsoleAppender whereas “File”, “MaxFileSize”, “MaxBackupIndex” are parameters of RollingFileAppender.

<appender name="console" class="org.apache.log4j.ConsoleAppender">
	<param name="Target" value="System.out" />
	<layout class="org.apache.log4j.PatternLayout">
		<param name="ConversionPattern" value="%-5p %c{1} - %m%n" />

<!-- rolling file appender -->
<appender name="file" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="logs/main.log" />
    <param name="Append" value="true" />
    <param name="ImmediateFlush" value="true" />
    <param name="MaxFileSize" value="10MB" />
    <param name="MaxBackupIndex" value="5" />

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" />

log4j.xml example – layout

Next part is layout where we define logging pattern through PatternLayout parameter ConversionPattern. This gets prefixed to all the logging messages. In above file appender, we are prefixing log messages with date, thread name, class name and line number. Below is a sample output for this pattern:

2016-05-12 21:22:44,610 +0530 [main] DEBUG (Log4jExample.java:18) - XYZ Message

log4j xml configuration filter and logger

Next is the filter section where we can define our own custom filters or use any existing filters, for more details read log4j filters.

Next part is logger, we can have multiple loggers too. The “name” attribute is used to define the package where this logger will be used. Note that if there are multiple logger matches to a class, then most specific one is used.

log4j xml additivity

The “additivity” attribute is a very important one – if it’s true then logging goes through hierarchy. For example if we have loggers defined as below.

<logger name="com.journaldev.log4j" additivity="true">
	<level value="DEBUG" />
	<appender-ref ref="jdbc" />

<logger name="com.journaldev.log4j.model" additivity="true">
	<level value="DEBUG" />
	<appender-ref ref="file" />

	<priority value="DEBUG" />
	<appender-ref ref="console" />

And we are logging from some class in com.journaldev.log4j.model package, then it will be using appenders “file”, “jdbc” and “console” because of logger hierarchy. This can generate a lot of redundant logs, so we usually keep it false. It’s default value is “true” though.

A logger can use multiple appenders too.

<logger name="com.journaldev.log4j" additivity="true">
	<level value="TRACE" />
	<appender-ref ref="console" />
	<appender-ref ref="file" />

log4j xml root logger

Finally root logger needs to be defined, this is the default level and appenders being used if there are no logger match found.

Before I conclude this tutorial, one more point is that log4j framework looks for log4j.xml or log4j.properties file in the classpath. If you are using some different name for these configuration files then you need to configure them before using it.

// OR, don't use both

For standalone java programs, you can do it in main method before program starts execution. For web applications, you can do it through Servlet Context Listener classes.


Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages