Log4j

Apache Log4j was the most popular logging framework for several years. It introduced the basic concept of hierarchical log levels and loggers, which are still used in modern logging frameworks. It is a trustworthy, quick, and adaptable logging framework written in Java and disseminated under the Apache Software Licence. It is an open-source logging framework.

Log4j is thread-safe and optimized for speed. It is easy to alter the format of the logger messages by extending the Layout class. Log4j 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 Log4j

Add the maven dependency for log4j in the pom.xml file as given below.
https://github.com/vmstate/article-100/blob/main/java/log4j/pom.xml

Logger

The logger object is declared in the following format.

private static final Logger logger = Logger.getLogger(Log4jExample.class);
https://github.com/vmstate/article-100/blob/main/java/log4j/src/main/java/vmstate/log4j/case001/Log4jExample.java

All levels of logger messages are added in the above example. We can decide on which logger messages to be passed to the appender. It is configured on the log4j.properties file. We can specify the log level in the field log4j.rootLogger.

log4j.rootLogger=INFO, console
https://github.com/vmstate/article-100/blob/main/java/log4j/src/main/resources/log4j.properties

Here we have set the root logger level as INFO and the appender as ConsoleAppender. 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

Filters allow processing the log messages based on selected criteria. Log4j supports filters at the logger level and the appender level. In the below-given example, we have added a filter in the appender. Include the following code in the log4j.properties to achieve the filtering.

log4j.appender.consoleAppender.filter.1=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.consoleAppender.filter.1.LevelToMatch=WARN
log4j.appender.consoleAppender.filter.2=org.apache.log4j.varia.DenyAllFilter

Here only the WARN messages will be printed in the console.

Appender

Appenders are used to define the destination of the logger messages. Log4j has different appenders, such as ConsoleAppender, FileAppender, and RollingFileAppender.

In this example, we have used ConsoleAppender to print the logger messages to the console.

log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender

Layout

Layout is used to format the log events before they are given to the appenders. Log4j provides various appenders such as PatternLayout, HTMLLayout, and XMLLayout.

log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%d %p %c - %m%n

In the above-given example, PatternLayout is used to convert the logger messages in a pattern with date, package information, and message.

Post a comment