Log4j2

Log4j2 provides extensive logging capabilities, including support for various log levels, flexible configuration options, and customizable appenders and layouts. Log4j2 offers improved performance and scalability compared to its predecessor, Log4j. Similar to log4j, log4j2 is thread-safe and optimized for speed. It is easy to alter the format of the logger messages by extending the Layout class. It is designed to manage Java exceptions from the start. The behavior of logging can be set at runtime using a configuration file.

Getting started with Log4j2

To use Log4j2 in our Java project, we must include the corresponding dependencies in the pom.xml file.

https://github.com/vmstate/article-100/blob/main/java/log4j2/pom.xml

The "log4j-api" provides a public API for the logging framework. It includes classes and interfaces for logging messages, defining loggers, configuring log levels, and handling log events.

The "log4j-core" explicitly provides the core functionality of Log4j, including the LogManager, LoggerContext, Appender classes, and others.

Logger

The logger captures the log records and passes them to the corresponding Appender. In the below-given example, a logger object is declared, and log records are added.

https://github.com/vmstate/article-100/blob/main/java/log4j2/src/main/java/com/vmstate/log4j2/case001/Log4j2Example.java

The root logger level can be configured in the log4j2.xml file.

https://github.com/vmstate/article-100/blob/main/java/log4j2/src/main/resources/log4j2.xml

In the above xml file, we have used console appender. The logger messages of different levels will appear on the console based on the value of the root level.

Root Level Logger Levels
TRACE TRACE, INFO, DEBUG, WARN, ERROR, FATAL
INFO INFO, WARN, ERROR, FATAL
DEBUG INFO, DEBUG, WARN, ERROR, FATAL
WARN WARN, ERROR, FATAL
ERROR ERROR, FATAL
FATAL FATAL

Filters

Filter is a component that decides whether the LogRecord should be forwarded to the output. In our example, we have added a threshold filter in the log4j2.xml file. Log4j2 uses different filters, including level filters, marker filters, and custom filters. Filters can be applied at the logger level or the appender level.

<Appenders>
        <Console name="Console" target="SYSTEM_OUT">
        	<Filters>
                <ThresholdFilter level="ERROR" onMatch="ACCEPT" 
                    onMismatch="DENY"/>
            </Filters>
        </Console>
    </Appenders>

In the above example, we have added a filter in the appender. It removes all the logger levels below ERROR from the appender.

Appenders

Appenders are responsible for outputting log messages to different destinations like console, files, databases, or other custom destinations. Log4j2 provides different appenders such as Console Appender, File Appender, RollingFile Appender, SMTP Appender, Socket Appender, etc.

 <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
        	<Filters>
                <ThresholdFilter level="ERROR" onMatch="ACCEPT" 
                 onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - 
              %msg%n"/>
        </Console>
    </Appenders>

The above given code indicates an appender. The appender can is defined in the log4j2.xml file. Here the appender is defined as a console appender to print the logger messages to the console. A filter is added to the appender to remove the logger messages below ERROR level. The layout of the logger messages is also added to the appender.

Layout

In Log4j2, layouts are responsible for formatting log messages before passing them to appenders. Layouts define the structure and content of log messages, including timestamp, log level, logger name, and the actual log message. Log4j2 provides multiple built-in layouts, such as PatternLayout, JSONLayout, XMLLayout, and HTMLLayout.

In our example, we have used PatternLayout to configure the logger message.

<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>

Here the pattern layout defines the pattern of the logger messages.

Post a comment

Comments

Sam Cameron 16-Feb-2024, 03:49:29 AM

This article offers a clear understanding of how Log4j2 operates within a logging framework.

Sam Cameron 16-Feb-2024, 04:57:56 AM

Also used PatternLayout to configure the logger message.

Sam Cameron 16-Feb-2024, 03:05:49 AM

Explains how to integrate Log4j2 into a Java project, outlining the necessary dependencies and configuration steps.

Sam Cameron 16-Feb-2024, 03:04:32 AM

Offering both theoretical insights and practical implementation is good.

Sam Cameron 16-Feb-2024, 03:03:49 AM

This article serves as a valuable resource for developers looking to leverage Log4j2 for logging in Java applications

Sam Cameron 16-Feb-2024, 03:02:08 AM

Good highlights of robust features and advantages of Log4j2

Load More
. . . . .