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.xmlThe "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.javaThe 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.xmlIn 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.
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.
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.
Here the pattern layout defines the pattern of the logger messages.
Post a comment