Log4j HTMLLayout – Create Logs in HTML Format

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 html 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 HTMLLayout in log4j.properties file

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

This provided the following information to be displayed:

  • The time elapsed from the start of the application before a particular logging event was generated.
  • The name of the thread that invoked the logging request.
  • The level associated with this logging request.
  • The name of the logger and logging message.
  • The optional location information for the program file and the line number from which this logging was invoked.
# Define the root logger with file appender
log4j.rootLogger = DEBUG, HTML

# Define the file appender

# Define the html layout for file appender
log4j.appender.HTML.layout.Title=Application logs

Step 3) Configure log4j.properties and test the application

package com.howtodoinjava;

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

public class Log4jHTMLLayoutExample
	static Logger log = Logger.getLogger(Log4jHTMLLayoutExample.class);

	public static void main(String[] args)

		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.html in project root folder:

HTML logs generated from log4j

Let me know if any question.

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.

8 thoughts on “Log4j HTMLLayout – Create Logs in HTML Format”

  1. Hi Lokesh,
    How can I customize HTML output file? I want to add the timestamp column in the file, please let me know how can I achieve this?

  2. Hello Lokesh,

    I have a questing on the same. Can we change the log4j output format ? like attached Html code with the comment ?

    Html Code :

    Application log

    Log session start time Thu Nov 17 17:45:01 IST 2016

    Expected Result
    Actual Result

    Testing Cases
    WS_TC_30: Actual Result
    WS_TC_30: Expected Result

  3. What I see is that if a I use newline (n) or any html tag they are skipped or printed out as they are (ex. for html tag)
    Is there a way to print out the messages with formatted text ?

    • As far I see, org.apache.log4j.HTMLLayout class is responsible for removing the formatting and uses org.apache.log4j.helpers.Transform.escapeTags() method for removing the formatting from log messages. It does not check any configuration option, so I believe that using configuration it’s not possible.

      A valid work around is to rewrite “org.apache.log4j.HTMLLayout” class again as per your need (basically comment the lines where escapeTags() has been called), generate the .class file, and put this .class file in your class path.

      Class loader will pickup the .class file from class-path and will use it for writing the logs.

  4. Hello Lokesh,

    Following is my log4j property file & I am getting :
    ERROR: invalid console appender config detected, console stream is looping

    log4j.rootLogger=debug, stdout, R


    # Pattern to output the caller’s file name and line number.
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) – %m%n


    # Keep one backup file

    log4j.appender.R.layout.ConversionPattern=%p %t %c – %m%n

    Can you please suggest that what I need anymore to output in CONSOLE as well as in LOG FILE.

  5. Hello Lokesh,
    I have a questing on the same. Can we change the log4j output format ?
    I need to add some parameter while calling ‘log.error()’ method like userdefined error code. & its description.
    Is it possible ?


Leave a Comment


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