Log4j XMLLayout – Create Logs in XML File

Log4j is a simple and flexible logging framework. Logging equips the developer with detailed context for application failures. With log4j it is possible to enable logging at runtime without modifying the application binary. The log4j package is designed so that these statements can remain in shipped code without incurring a heavy performance cost.

Log4j comes with multiple options to format log files created by framework. It can create simple log files, html log files or xml log files also.

This this post, I am showing the example code for configuring log4j to produce logs in xml format.

Step 1) Create a maven java project and update log4j dependencies

Follow the steps given in this post related to configuring log4j with maven.

Step 2) Configure XMLLayout in log4j.properties file

The XMLLayout class extends the abstract org.apache.log4j.Layout class and overrides the format() method from its base class to provide XML-style formatting.

This provided the following information in xml tags:

  1. log4j:event :This tag has information about logger settings like logger name, timestamp, level and thread.
  2. log4j:message : It contains actual log message in CDATA format. e.g.
  3. log4j:locationInfo : It contains the location of log statement in class file.
# Define the root logger with file appender
log4j.rootLogger = DEBUG, XML

# Define the file appender
log4j.appender.XML=org.apache.log4j.FileAppender
log4j.appender.XML.File=application.xml

# Define the xml layout for file appender
log4j.appender.XML.layout=org.apache.log4j.xml.XMLLayout
log4j.appender.XML.layout.LocationInfo=true
log4j.appender.XML.Threshold=DEBUG

Step 3) Configure log4j.properties and test the application

package com.howtodoinjava;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Log4jXMLLayoutExample
{
	static Logger log = Logger.getLogger(Log4jXMLLayoutExample.class);

	public static void main(String[] args)
	{
		PropertyConfigurator.configure("log4j.properties");

		log.debug("Sample debug message");
		log.info("Sample info message");
		log.error("Sample error message");
		log.fatal("Sample fatal message");
	}
}

Output will be logged in application.xml file in project root folder. Sample content will be like this:

<log4j:event logger="com.howtodoinjava.Log4jXMLLayoutExample" timestamp="1368417841874" level="DEBUG" thread="main">
	<log4j:message><!&#91;CDATA&#91;Sample debug message&#93;&#93;></log4j:message>
	<log4j:locationInfo class="com.howtodoinjava.Log4jXMLLayoutExample" method="main" file="Log4jXMLLayoutExample.java" line="14"/>
</log4j:event>

<log4j:event logger="com.howtodoinjava.Log4jXMLLayoutExample" timestamp="1368417841893" level="INFO" thread="main">
	<log4j:message><!&#91;CDATA&#91;Sample info message&#93;&#93;></log4j:message>
	<log4j:locationInfo class="com.howtodoinjava.Log4jXMLLayoutExample" method="main" file="Log4jXMLLayoutExample.java" line="15"/>
</log4j:event>

<log4j:event logger="com.howtodoinjava.Log4jXMLLayoutExample" timestamp="1368417841893" level="ERROR" thread="main">
	<log4j:message><!&#91;CDATA&#91;Sample error message&#93;&#93;></log4j:message>
	<log4j:locationInfo class="com.howtodoinjava.Log4jXMLLayoutExample" method="main" file="Log4jXMLLayoutExample.java" line="16"/>
</log4j:event>

<log4j:event logger="com.howtodoinjava.Log4jXMLLayoutExample" timestamp="1368417841893" level="FATAL" thread="main">
	<log4j:message><!&#91;CDATA&#91;Sample fatal message&#93;&#93;></log4j:message>
	<log4j:locationInfo class="com.howtodoinjava.Log4jXMLLayoutExample" method="main" file="Log4jXMLLayoutExample.java" line="17"/>
</log4j:event>

Important point

If you will try to view above file in browser the it will show you the parse error : “XML Parsing Error: prefix not bound to a namespace“. This is expected because log file does not contain any root element.

Also according to java docs of XMLLayout, “The output of the XMLLayout consists of a series of log4j:event elements as defined in the log4j.dtd. It does not output a complete well-formed XML file. The output is designed to be included as an external entity in a separate file to form a correct XML file.”

For example, if abc is the name of the file where the XMLLayout output goes, then a well-formed XML file would be:

<!DOCTYPE log4j:eventSet PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd" &#91;<!ENTITY data SYSTEM "abc">]>
<log4j:eventSet version="1.2" xmlns:log4j="http://jakarta.apache.org/log4j/">

	<log4j:event logger="com.howtodoinjava.Log4jXMLLayoutExample" timestamp="1368417841874" level="DEBUG" thread="main">
		<log4j:message><!&#91;CDATA&#91;Sample debug message&#93;&#93;></log4j:message>
		<log4j:locationInfo class="com.howtodoinjava.Log4jXMLLayoutExample" method="main" file="Log4jXMLLayoutExample.java" line="14"/>
	</log4j:event>

	<log4j:event logger="com.howtodoinjava.Log4jXMLLayoutExample" timestamp="1368417841893" level="INFO" thread="main">
		<log4j:message><!&#91;CDATA&#91;Sample info message&#93;&#93;></log4j:message>
		<log4j:locationInfo class="com.howtodoinjava.Log4jXMLLayoutExample" method="main" file="Log4jXMLLayoutExample.java" line="15"/>
	</log4j:event>

	<log4j:event logger="com.howtodoinjava.Log4jXMLLayoutExample" timestamp="1368417841893" level="ERROR" thread="main">
		<log4j:message><!&#91;CDATA&#91;Sample error message&#93;&#93;></log4j:message>
		<log4j:locationInfo class="com.howtodoinjava.Log4jXMLLayoutExample" method="main" file="Log4jXMLLayoutExample.java" line="16"/>
	</log4j:event>

	<log4j:event logger="com.howtodoinjava.Log4jXMLLayoutExample" timestamp="1368417841893" level="FATAL" thread="main">
		<log4j:message><!&#91;CDATA&#91;Sample fatal message&#93;&#93;></log4j:message>
		<log4j:locationInfo class="com.howtodoinjava.Log4jXMLLayoutExample" method="main" file="Log4jXMLLayoutExample.java" line="17"/>
	</log4j:event>
	
</log4j:eventSet>

This approach enforces the independence of the XMLLayout and the appender where it is embedded.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.