Log4j 2 Best Practices Example

Logging is a critical feature of any application. In this tutorial, we will cover some Log4j2 best practices that can help developers get started and improve the logging with Log4j2.

1. Introduction

Printing messages to the console is an integral part of the development testing and the debugging of a Java program. If developers are working on a Server side application, where they cannot see what’s going on inside the server, then their only visibility tool is a log file.

Without logs, developers cannot do any debugging or see what’s going on inside the application. Though, Java has pretty handy System.out.println() methods to print something on console, which can also be routed to log file but not sufficient for a real-world Java application.

If developers are running a Java program in Linux or Unix based systems, Log4j or SLF4j or any other logging framework offers a lot more features, flexibility, and improvement on message quality, which is not possible using the System.out.println() statements.

1.1 What is Log4j2?

Log4j2 is the updated version of the popular and influential Log4j library, which is simple, flexible, and fast Java-based logging framework. It is thread-safe and supports internationalization. We mainly have 3 components to work with Log4j:

1.1.1 Log4j2 Logger Class

Logger class provides the methods for the logging process. We can use the LogManager.getLogger() method to get the Logger object. The syntax is given below:

static Logger log = LogManager.getLogger(YourClassName.class);

Logger class has 6 different logging methods which are used to print the status of an application:

Description Method Syntax
debug(Object message) It is used to print the message with the level org.apache.logging.log4j.Level.DEBUG. It is the lowest restricted logging level. public void debug(Object message)
info(Object message) It is used to print the message with the level org.apache.logging.log4j.Level.INFO. It is more restricted than the DEBUG logging level and developers should log messages which are for an informative purpose. public void info(Object message)
warn(Object message) It is used to print the message with the level org.apache.logging.log4j.Level.WARN. It is more restricted than the INFO logging level and is used to log the warning sort of messages i.e. Connection lost between Client and Server, Database Connection lost etc. public void warn(Object message)
error(Object message) It is used to print the message with the level org.apache.logging.log4j.Level.ERROR. It is more restricted than the WARN logging level and is used to log errors and exceptions. public void error(Object message)
fatal(Object message) It is used to print the message with the level org.apache.logging.log4j.Level.FATAL. public void fatal(Object message)
trace(Object message) It is used to print the message with the level org.apache.logging.log4j.Level.TRACE. public void trace(Object message)

To summarize, the priority level is given below.

Trace < Debug < Info < Warn < Error < Fatal

Where org.apache.logging.log4j.Level.FATAL has the highest priority and org.apache.logging.log4j.Level.Trace the lowest.

1.1.2 Log4j2 Appender Interface

Appender is an interface which is primarily responsible for printing the logging messages to the different destinations such as console, files, sockets, database etc. In Log4j2 we have different types of Appender implementation classes:

Fig. 1: Log4j2 Appenders

1.1.3 Log4j Layout Class

Layout component specifies the format in which the log statements are written into the destination repository by the Appender. In Log4j2 we have different types of Layout implementation classes:

Fig. 2: Log4j2 Layout

1.2 Why prefer Log4j2 over System.out.println?

Below are some of the reasons, which are enough to understand the limitation of using System.out.println():

2. Log4j2 Best Practices

That’s all for this post. Happy Learning and don’t forget to share!!

3. Conclusion

These tips and examples on logging in Java are based on my experience and how I use the logging framework in Java. By no means, is not complete. I would love to hear some more tips from you guys and how you are using and customizing the Java logging.

Exit mobile version