Log4j2 HTMLLayout Configuration Example

This Log4j2 example will help you in configuring log4j2.xml file with HTMLLayout. The HTMLLayout generates an HTML page and adds each log statement to a row in a table.

1. HTMLLayout Configuration Options

You can use following attributes while configuring HTMLLayout:

  1. locationInfo – If “true”, location information will be included. The default is false.
  2. title – The title to include in the file header. If none is specified the default title will be used.
  3. contentType – The content type. Defaults to “text/html”.
  4. charset – The character set to use. If not specified, the default will be used.
  5. fontSize – The font size of the text.
  6. font – The font to use for the text.

Let’s see different configurations options for generating HTML format-based log files.

1.1. Log4j2.xml Configuration

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
        <Property name="basePath">C:\\logs</Property>
        <RollingFile name="fileLogger" fileName="${basePath}/app-info.html" 
            <HTMLLayout charset="UTF-8" title="Howtodoinjava Info Logs" locationInfo="true" />
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                <SizeBasedTriggeringPolicy size="10 MB" />
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout   pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
        <Logger name="com.howtodoinjava" level="debug" additivity="false">
            <appender-ref ref="fileLogger" level="debug" />
        <Root level="debug" additivity="false">
            <appender-ref ref="console" />

1.2. Log4j2.properties Configuration

status = error
name = PropertiesConfig
#Make sure to change log file path as per your need
property.filename = C:\\logs\\app-info.html
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = rolling
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.html.gz
appender.rolling.layout.type = HTMLLayout
appender.rolling.layout.charset = UTF-8
appender.rolling.layout.title = Howtodoinjava Info Logs
appender.rolling.layout.locationInfo = true
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
loggers = rolling
#Make sure to change the package structure as per your application
logger.rolling.name = com.howtodoinjava
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

1.3. Log4j2.json Configuration

   "configuration": {
      "name": "Default",
      "appenders": {
         "RollingFile": {
            "HTMLLayout": {
               "title":"Howtodoinjava Info Logs",
            "Policies": {
               "SizeBasedTriggeringPolicy": {
                  "size":"10 MB"
            "DefaultRolloverStrategy": {
      "loggers": {
         "root": {
            "appender-ref": {

2. Log Statements

Now let’s use dome log statements to generate log files.

package com.howtodoinjava.log4j2.examples;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4j2HelloWorldExample 
	private static final Logger LOGGER = LogManager.getLogger(Log4j2HelloWorldExample.class.getName());
	public static void main(String[] args) 
		LOGGER.debug("Debug Message Logged !!");
		LOGGER.info("Info Message Logged !!");
		LOGGER.debug("Another Debug Message !!");

3. Log4j2 HTMLLayout Output Example

Running the above log files will generate the following HTML file.

Log4j2 HTMLLayout Output
Log4j2 HTMLLayout Output

Drop me your questions in the comments section.




Notify of
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.