Log4j2 Asynchronous Logging

Adil Abdullah
3 min readJul 18, 2022

--

What to cover:

1. Log4j2 dependencies.

2. XML Configuration of Log4j2 file.

3. Define and appenders and message format in XML file.

4. Call logger in Java code.

5. Java code to print log in file.

Step 1:

Create Java maven project in Eclipse, Intelli or any other Java IDE.

Step 2:

Check (skip archetype selection) and click Next.

Step 3:

Write group id just like package name format. Artifact Id is the name of project. Name and description are optional. Remaining fields remain same and then click Finnish.

Step 4:

Project structure look like this below.

Step 5:

Open pom.xml file and add below dependencies in between <dependencies> tag.

<! — https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core

<dependency>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-core</artifactId>

<version>2.17.2</version>

</dependency>

<! — https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api

<dependency>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-api</artifactId>

<version>2.17.2</version>

</dependency>

<dependency>

<groupId>com.lmax</groupId>

<artifactId>disruptor</artifactId>

<version>3.3.4</version>

</dependency>

</dependencies>

Step 6:

Sample pom.xml look like below.

Pom.xml

<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.java.logging</groupId>

<artifactId>AsyncLogging</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>Java Log4j2 Logging</name>

<description>Asynchronous logging in Java using Log4j2</description>

<url>http://maven.apache.org</url>

<properties>

<! — https://maven.apache.org/general.html#encoding-warning

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.source>1.8</maven.compiler.source>

<maven.compiler.target>1.8</maven.compiler.target>

</properties>

<dependencies>

<! — https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core

<dependency>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-core</artifactId>

<version>2.17.2</version>

</dependency>

<! — https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api

<dependency>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-api</artifactId>

<version>2.17.2</version>

</dependency>

<dependency>

<groupId>com.lmax</groupId>

<artifactId>disruptor</artifactId>

<version>3.3.4</version>

</dependency>

</dependencies>

</project>

Step 7:

Create java class with name as you want and package but not on default package on our java project.

Step 8:

Create Log4j2 xml file on any location you want in your drive. Sample below just copy and paste just change name of appender and logger you want.

Log4j2.xml

<?xml version=”1.0" encoding=”UTF-8"?>

<Configuration status=”WARN” monitorInterval=”30">

<! — Logging Properties →

<Properties>

<Property name=”LOG_PATTERN”>%d{yyyy-MM-dd’T’HH:mm:ss.SSSZ} %p %m%n</Property>

<Property name=”APP_LOG_ROOT”>D://Misc//Logs2</Property>

</Properties>

<Appenders>

<! — Console Appender →

<Console name=”Console” target=”SYSTEM_OUT” follow=”true”>

<PatternLayout pattern=”${LOG_PATTERN}”/>

</Console>

<! — File Appenders on need basis →

<RollingFile name=”file_1" fileName=”${APP_LOG_ROOT}/app-1.log”

filePattern=”${APP_LOG_ROOT}/app-framework-%d{yyyy-MM-dd}-%i.log”>

<PatternLayout pattern=”${LOG_PATTERN}”/>

<Policies>

<SizeBasedTriggeringPolicy size=”19500KB” />

</Policies>

<DefaultRolloverStrategy max=”10"/>

</RollingFile>

</Appenders>

<Loggers>

<Logger name=”File1" additivity=”false” level=”info”>

<AppenderRef ref=”file_1" />

</Logger>

<Root level=”warn”>

<AppenderRef ref=”Console”/>

<AppenderRef ref=”file_1"/>

</Root>

</Loggers>

</Configuration>

Step 9:

Write below code on Java class.

Logging.java

package com.java.log;

import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

public class Logging {

static {

System.setProperty(“log4j.configurationFile”, <path of your log4j2 xml created on step 8>);

/*Enabling async logging.*/

System.setProperty(“log4j2.contextSelector”, “org.apache.logging.log4j.core.async.AsyncLoggerContextSelector”);

}

public static void main(String[] args)

{

printLog(<Name of logger defined in log4j2 xml file>,

<Message you want to print for log>);

}

public static void printLog(String loger,String message)

{

Logger logger =LogManager.getLogger(loger);

logger.info(message.trim());

}

}

Step 10:

Run the program and check log is created or not. If log file is created then open and see the message is print or not. Below is the sample message format print on log file.

Sample log file message format.

2022–07–01T23:42:02.045+0500 INFO Adil Abdullah Log Message 1

2022–07–01T23:42:04.622+0500 INFO Adil Abdullah Log Message 1

2022–07–01T23:42:05.853+0500 INFO Adil Abdullah Log Message 1

2022–07–01T23:42:06.875+0500 INFO Adil Abdullah Log Message 1

--

--

Adil Abdullah

Currently working as Full stack Java developer at Contour Software at their Easit AB division part of Jonas group. Working on Java and IBM stack.