HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Log4j / Log4j XMLLayout – Create Logs in XML File

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?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Log4j2 Tutorial

  • Log4j2 – Introduction
  • Log4j2 – JSON Config
  • Log4j2 – Properties Config
  • Log4j2 – XML Config
  • Log4j2 – ConsoleAppender
  • Log4j2 – RollingFileAppender
  • Log4j2 – Multiple appenders
  • Log4j2 – LevelRangeFilter
  • Log4j2 – HTMLLayout
  • Log4j2 – Fish Tagging
  • Log4j2 – Conversion Patterns
  • Log4j2 – JUnit

Log4j Tutorial

  • Log4j – Introduction
  • Log4j – Properties Config
  • Log4j – XML Config
  • Log4j – Maven Config
  • Log4j – Logging Levels
  • Log4j – ConsoleAppender
  • Log4j – RollingFileAppender
  • Log4j – SocketAppender
  • Log4j – JDBCAppender
  • Log4j – XMLLayout
  • Log4j – HTMLLayout
  • Log4j – Runtime Reload
  • Log4j vs. SLF4j
  • Log4j – RESTEasy + Tomcat 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)