Logging using Log4j2
Log4j2 is one of the most common logging frameworks used with Java. Since SLF4J is an abstraction of logging frameworks, Log4j2 can be used with SLF4J. Log4j2 is very flexible and offers different ways to store log information for debugging; it also supports asynchronous logging and displays logs with a severity level to quickly identify the importance of messages.
Let’s discuss the following features of Log4j2:
- The Log4j2 Logger
- Log4j2 Appenders
- Log4j2 Layouts
- Log4j2 Markers
- Log4j2 Filters
The Log4j2 Logger
The Logger is the main feature used by our application to create LogRecord
instances. This means the logger is responsible for dispatching the messages. To create a Log4j2 Logger, we only need the following code:
Logger log = LogManager.getLogger(ExampleClass.class);
After creating a new Logger, we can now use it to call several methods, such as info()
, to dispatch messages.
Log4j2 Appenders
Appenders...