LOG4J 2 in maven

Every applications in production environment should have method of debugging and trace the application state, which helps to verify or identify the issues. Here it comes to the logging the application. Since logging is less focus on application, the log4j gives an advantage, which because it is simple to understand and to use.

Log4j 2 is asynchronous based logger unless log4j 1.x.

In multi-threaded applications asynchronous Loggers have 10 times higher output and has a lower latency than Log4j 1.x and Logback.

Let’s see how to config Log4j2 in maven application.

Add the below dependencies in pom.xml — required

<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>

If existing components use Log4j 1.x and you want to have this logging routed to Log4j 2, then remove any log4j 1.x dependencies and add the following.

<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>

If existing components use Apache Commons Logging 1.x and you want to have this logging routed to Log4j 2, then add the following but do not remove any Commons Logging 1.x dependencies.

<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>

If existing components use SLF4J and you want to have this logging routed to Log4j 2, then add the following but do not remove any SLF4J dependencies.

<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>

If your application is using servlets. This module is only required at runtime.

<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>

If your application is using JSP s. The Log4j Log Tag Library creates the capability of inserting log statements in JSPs without the use of Java scripting.

<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-taglib</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>

The Log4j 2 to SLF4J Adapter allows applications coded to the Log4j 2 API to be routed to SLF4J. Use of this adapter may cause some loss of performance as the Log4j 2 Messages must be formatted before they can be passed to SLF4J. The SLF4J Bridge must NOT be on the class path when this is in use.

<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>

If your configuration uses the NoSQL CouchDB appender, then add the following.

<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-couchdb</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>

If your configuration uses the NoSQL MongoDB appender, then add the following.

<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-mongodb</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>

If your configuration uses the Cassandra appender, then add the following.

<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-cassandra</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>

Configuration

Configuration file can be written in XML, JSON, YAML, or properties format.

Here is a console appender as xml file (Log4j2.xml).

<?xml version=”1.0" encoding=”UTF-8"?>
<Configuration status=”WARN”>
<Appenders>
<Console name=”Console” target=”SYSTEM_OUT”>
<PatternLayout pattern=”%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} — %msg%n”/>
</Console>
</Appenders>
<Loggers>
<Root level=”error”>
<AppenderRef ref=”Console”/>
</Root>
</Loggers>
</Configuration>

Now all the configurations has been done. You can use it in your application.

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@RestController
public class MainController {
private static final Logger logger = LogManager.getLogger(MainController.class);

@GetMapping(value = “/test-log”)
public String getIndex() {
logger.trace(“A TRACE Message”);
logger.debug(“A DEBUG Message”);
logger.info(“An INFO Message”);
logger.warn(“A WARN Message”);
logger.error(“An ERROR Message”);
return “test”;
}
}