- log4j - Home
- log4j - Overview
- log4j - Installation
- log4j - Architecture
- log4j - Configuration
- log4j - Sample Program
- log4j - Logging Methods
- log4j - Logging Levels
- log4j - ConsoleAppender
- log4j - Logging in Database
Log4j - Formatting Layouts
- log4j - Log Formatting
- log4j - CSV Parameter Layout
- log4j - CSV Log Event Layout
- log4j - HTML Layout
- log4j - Pattern Layout
- log4j - Json Template Layout
Log4j - File Appenders
- log4j - Logging in Files
- log4j - FileAppender
- log4j - Separate Folder per Month
- log4j - Daily log File Generation
log4j Resources
log4j - HTMLLayout
If you want to generate your logging information in an HTML-formatted file, then you can use org.apache.logging.log4j.HTMLLayout to format your logging information.
The HTMLLayout class extends the abstract org.apache.logging.log4j.Layout class and overrides the format() method from its base class to provide HTML-style formatting.
It provides 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.
HTMLLayout Methods
HTMLLayout is a very simple Layout object that provides the following methods:
| Sr.No. | Method & Description |
|---|---|
| 1 |
setContentType(String)
Sets the content type of the text/html HTML content. Default is text/html. |
| 2 |
setLocationInfo(String)
Sets the location information for the logging event. Default is false. |
| 3 |
setTitle(String)
Sets the title for the HTML file. Default is log4j Log Messages. |
Example - Usage of HTMLLayout
Following is a simple configuration file for HTMLLayout:
log4j2.properties
// Define a File Appender with HTMLLayout appender.0.type = File appender.0.name = MAIN appender.0.fileName = logs/htmlLayout.html appender.0.layout.type = HTMLLayout appender.0.layout.Title=HTML Layout Example appender.0.layout.LocationInfo=true rootLogger.level = DEBUG // Attach the appender to rootLogger rootLogger.appenderRef.0.ref = MAIN rootLogger.appenderRef.0.level = INFO
Now consider the following Java Example which would generate logging information:
Log4jDemo.java
package com.tutorialspoint;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Log4jDemo{
/* Get actual class name to be printed on */
private static final Logger LOGGER = LogManager.getLogger();
public static void main(String[] args) {
LOGGER.debug("Hello this is an debug message");
LOGGER.info("Hello this is an info message");
}
}
Output
Compile and run the above program. It would create an htmlLayout.html file in logs directory which would have the following log information:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8"/>
<title>HTML Layout Example</title>
<style type="text/css">
<!--
body, table {font-family:arial,sans-serif; font-size: medium
;}th {background: #336699; color: #FFFFFF; text-align: left;}
-->
</style>
</head>
<body bgcolor="#FFFFFF" topmargin="6" leftmargin="6">
<hr size="1" noshade="noshade">
Log session start time Fri Sep 05 20:06:22 IST 2025<br>
<br>
<table cellspacing="0" cellpadding="4" border="1" bordercolor="#224466" width="100%">
<tr>
<th>Time</th>
<th>Thread</th>
<th>Level</th>
<th>Logger</th>
<th>File:Line</th>
<th>Message</th>
</tr>
<tr>
<td>637</td>
<td title="main thread">main</td>
<td title="Level">INFO</td>
<td title="com.tutorialspoint.Log4jDemo logger">com.tutorialspoint.Log4jDemo</td>
<td>Log4jDemo.java:14</td>
<td title="Message">Hello this is an info message</td>
</tr>
</table>
<br>
</body></html>
You would use a web browser to open htmlLayout.html file.
One of the big advantages of having the log file in HTML format is that it can be published as a web page for remote viewing.