Logging is an essential component of any application development process. It allows developers to track and monitor the flow of execution, identify errors and exceptions, and gain valuable insights into the application’s behavior. In this article, we will explore how to set up logging in a Java application using the popular logging framework, Log4j.
Step 1: Add Log4j Dependency
The first step is to add the Log4j dependency to your Java project. You can do this by adding the following dependencies to your project’s pom.xml
file if you are using Maven.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.0</version>
</dependency>
Alternatively, if you are using Gradle, add the below lines to your Gradle build.gradle
file.
implementation 'org.apache.logging.log4j:log4j-core:2.17.0'
implementation 'org.apache.logging.log4j:log4j-api:2.17.0'
Make sure to sync your project with the updated dependencies.
Step 2: Configure Log4j
Next, you need to configure Log4j to define how and where the logs should be generated. Create a log4j2.xml
file (name it as required) in your project’s resource directory (src/main/resources
) and add the following configuration:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="FileAppender" fileName="logs/application.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="FileAppender"/>
</Root>
</Loggers>
</Configuration>
This configuration sets up two appenders: ConsoleAppender
and FileAppender
. The `C