Logback HTMLLayout

Logback HTMLLayout outputs events in an HTML table. The content of the table columns is specified using a conversion pattern in the encoder property.

1. HTMLLayout Sample Configuration

For quick reference, this is the configuration file we will be referring. It logs into c:/temp/logs/applicationLogs.html file and rolls-over daily.

	<property name="LOG_ROOT" value="c:/temp/logs" />
	<property name="HTML_LOG_FILE" value="applicationLogs" />
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
	<appender name="HTML" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
			<!-- daily rollover -->
			<!-- each archived file's size will be max 10MB -->
			<!-- 30 days to keep -->
			<!-- total size of all archive files, if total size > 100GB, it will delete old archived file -->
		<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
			<layout class="ch.qos.logback.classic.html.HTMLLayout">
	<logger name="com.howtodoinjava.app" level="DEBUG" additivity="false">
        <appender-ref ref="HTML"/>

	<root level="INFO">
		<appender-ref ref="STDOUT" />
		<appender-ref ref="HTML" />

2. Dependencies

Logback requires three modules in the application runtime i.e. logback-corelogback-classic and slf4j-api.


Note that logback-classic will automatically pull in the logback-core and slf4j-api, so adding logback-classic dependency is enough.

3. HTMLLayout

3.1. Default Conversion Pattern

By default, the HTMLLayout uses the following conversion pattern.


It creates a table containing the following data:

  • Date – Log event date time.
  • Thread – Thread name.
  • Level – Log level.
  • Logger – Logger name.
  • MDC – MDC (Mapped Diagnostic Context) information, if available.
  • Message – Log message.

We can customize the columns and information using the custom pattern in the encoder property.

It seems that Logback does not support the extensive table formatting as supported by Log4j2 HTMLLayout.

3.2. Custom Pattern Demo

We are using the custom pattern for demo purpose. We have removed MDC info, rather we are printing the line number in the classes.


Let’s quickly write a few log statements, and see the output.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.howtodoinjava.demo.lombok.Article;

public class Main {
	public static void main(final String[] args) {
		Logger logger = LoggerFactory.getLogger(Main.class);

		logger.debug("Debug Message Logged !!!");
		logger.info("Info Message Logged !!!");
		logger.error("Error Message Logged !!!", new NullPointerException("Something is NULL"));

		Article a = Article.builder(1L).title("Test Article").tag("Data").build();
		logger.info("Article fecthed for id : {} is : {}", 1, a);

Open the Log output file in the browser.

4. Conclusion

In this logback HTML Layout tutorial, we learned to create logs in HTML files with rolling capabilities. These HTML formatted logs can be helpful in generating nice-looking reports for sending the logs in the emails with SMTPAppender.

But if logs are being generated in very high amounts then we should be logging plain string logs, use specialized tools (such as Splunk) for log processing.

Happy Learning !!

Download Sourcecode


Notify of
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.