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.
<configuration>
<property name="LOG_ROOT" value="c:/temp/logs" />
<property name="HTML_LOG_FILE" value="applicationLogs" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="HTML" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_ROOT}/${HTML_LOG_FILE}.html</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_ROOT}/${HTML_LOG_FILE}-%d{yyyy-MM-dd}.%i.html.gz</fileNamePattern>
<!-- each archived file's size will be max 10MB -->
<maxFileSize>10MB</maxFileSize>
<!-- 30 days to keep -->
<maxHistory>30</maxHistory>
<!-- total size of all archive files, if total size > 100GB, it will delete old archived file -->
<totalSizeCap>100GB</totalSizeCap>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<charset>UTF-8</charset>
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%d{HH:mm:ss.SSS}%thread%level%logger%line%msg</pattern>
</layout>
</encoder>
</appender>
<logger name="com.howtodoinjava.app" level="DEBUG" additivity="false">
<appender-ref ref="HTML"/>
</logger>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="HTML" />
</root>
</configuration>
2. Dependencies
Logback requires three modules in the application runtime i.e. logback-core, logback-classic and slf4j-api.
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.10</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
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.
%date%thread%level%logger%mdc%msg
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.
%d{HH:mm:ss.SSS}%thread%level%logger%line%msg
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 !!