Log4j2 Maven Configuration

This post is regarding logging (log4j2) configuration in a maven project.

Here’s the steps to add lo4j2 in your maven project :-

Maven Project Structure

You need to have the above project structure .The log4j2.xml that is the logging configuration file ,needs to be placed under src/main/resources folder.

Dependencies

The following dependencies to be added for logging configuration.

Log4j Dependencies
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.2</version>
</dependency>
Jackson Dependencies

You also need to add the jackson dependencies.If you don’t add these dependencies you will getting the following error while running your application.Log4j require these dependencies.

Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ser.FilterProvider
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
… 21 more

Log4j XML Configuration

You need to create log4j configuration file and place it under src/main/resources .

Rolling File Appender

I have created two Rolling File appenders

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE" name="web" monitorInterval="30" >
<Appenders>
<RollingFile name="RollingFile" fileName="/Users/ensiahusain/var/log/httpd/log4jexample/log4jexample.log"
filePattern="/Users/ensiahusain/var/log/httpd/log4jexample/log4jexample-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>%d{ISO8601},%-5p\ [%c],[%X],%m,%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
</RollingFile>
<RollingFile name="AuditLogAppender" fileName="/Users/ensiahusain/var/log/httpd/log4jexample/log4jexample.log"
filePattern="/Users/ensiahusain/var/log/httpd/log4jexample/log4jexample-%d{yyyy-MM-dd}-%i.log">
<JsonLayout locationInfo="true" complete="false" charset="UTF-8" compact="true" eventEol="true" properties="true"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="RollingFile"/>
</Root>
<Logger name="AuditLogger" level="INFO" additivity="false">
<AppenderRef ref="AuditLogAppender"/>
</Logger>
</Loggers>
</Configuration>

Running the Java Application

Create a java application and create a logger instance and try to add some logs for testing the logging configuration.

After you run this java application logs should be created at the file location given in the log4j xml file.

Logging File

That’s all about log4j2 configuration.Happy Coding 🙂

Leave a comment

Your email address will not be published. Required fields are marked *