In this post, We look at Log4j 2 simple example.
Log4j2 is the updated version of the popular Apache library. It is used extensively throughout Java applications for so many years. The Apache Log4j 2 carries all the basic logging features of its predecessor and is built with some important improvements, importantly in the area of performance.
And of course, As you know how instrumental logging is for any application, both for debugging and audit purposes, selecting a solid logging library is quite an important decision for any application.
In the following sections, we’re going to take a look at how to use the log4j2 library.
pom.xml
We have added log4j2 log4j-api & log4j-core dependency in the dependencies section.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials.org</groupId> <artifactId>Log4j2SimpleExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.17.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.17.1</version> </dependency> </dependencies> </project> |
log4j2.xml
We have to create a log4j2.xml configuration file on the classpath(src/main/resources):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn"> <Appenders> <!-- configuration of Console appender --> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> </Appenders> <Loggers> <!-- Here Root logger is referring to console appender --> <Root level="debug" additivity="false"> <AppenderRef ref="console" /> </Root> </Loggers> </Configuration> |
Let’s have a closer look at the tags used in the log4j2.xml configuration:
- Configuration: This is the root element in a log4j2 configuration file. The status attribute is used to represent the level at which internal log4j events should be logged
- Appenders: This element mainly holds a list of appenders. In our example, an appender corresponding to the System console(SYSTEM_OUT) is defined
- Loggers: This element usually holds a list of Logger instances. The Root element is a standard logger that is used to set the default log level to output all log messages
NOTE: It’s important to note that the Root logger is mandatory in every logger configuration file. if you don’t set a value for the root logger then it will automatically be configured by default with a Console appender and the ERROR log level.
ClientTest.java
Now we are going to test our logger. For this, I’ve written below test class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.kkjavatutorials.client; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class ClientTest { //Get Logger Object reference through LogManager private static final Logger logger = LogManager.getLogger(ClientTest.class); public static void main(String[] args) { logger.info("Logging info message"); logger.warn("Logging warn message"); logger.error("Logging error message"); } } |
If you run ClientTest.java as a java application then you’ll find the below output:
11:22:53.905 [main] INFO com.kkjavatutorials.client.ClientTest – Logging info message
11:22:53.906 [main] WARN com.kkjavatutorials.client.ClientTest – Logging warn message
11:22:53.907 [main] ERROR com.kkjavatutorials.client.ClientTest – Logging error message
You May Also Like:
Why Logging?
How to Create a Custom Appender in log4j2?
That’s All about Log4j 2 simple example