Configure Log4j with XML File
Configure Log4j with XML File
Log4j is a popular logging framework used in Java applications. It allows developers to capture detailed logs that are vital for debugging and monitoring the performance of applications. To effectively use Log4j, you need to configure its XML file, which dictates how logging is handled in your project. In this guide, we’ll walk you through the process of configuring the log4j.xml
file.
Download Log4j Jar File
You can download the Log4j jar file from the official Apache Log4j website.
If the project is using Maven, you can add the Maven dependency to the POM.xml file.
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
Create a Java Class
Create a simple Java class named LoggerExample.java
for logging demonstration.
Create log4j.xml File
This file defines logging configurations, including logging levels and appenders.
Attach Log4j Dependency to Classpath
Ensure that the Log4j library is added to your project’s classpath.
LoggerExample.java
package com.log;
import org.apache.log4j.Logger;
public class LoggerExample {
// Get a logger instance
public static Logger logger = Logger.getLogger(LoggerExample.class);
public void testLoggerDebug() {
logger.debug("Debug method");
}
public void testLoggerInfo() {
logger.info("Info method");
}
public void testLoggerWarn() {
logger.warn("Warn method");
}
public void testLoggerError() {
logger.error("Error method");
}
public void testLoggerFatal() {
logger.fatal("Fatal method");
}
public static void main(String[] args) {
LoggerExample example = new LoggerExample();
example.testLoggerDebug();
example.testLoggerInfo();
example.testLoggerWarn();
example.testLoggerError();
example.testLoggerFatal();
}
}
Understanding Log4j Appenders
Log4j allows logging requests to be sent to multiple destinations called appenders. The ConsoleAppender
prints log events to System.out
or System.err
.
Sample log4j.xml Configuration
<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Console Appender -->
<appender name="Console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<!-- Define the log pattern -->
<param name="ConversionPattern" value="%d{ISO8601} [%t] %-5p %c - %m%n"/>
</layout>
</appender>
<!-- File Appender -->
<appender name="File" class="org.apache.log4j.FileAppender">
<param name="File" value="logs/application.log"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- Define the log pattern for file logs -->
<param name="ConversionPattern" value="%d{ISO8601} [%t] %-5p %c - %m%n"/>
</layout>
</appender>
<!-- Root Logger -->
<root>
<level value="debug"/> <!-- Set log level to debug -->
<appender-ref ref="Console"/>
<appender-ref ref="File"/>
</root>
<!-- Custom Logger -->
<logger name="com.myapp">
<level value="info"/> <!-- Set log level to info for this package -->
<appender-ref ref="Console"/>
<appender-ref ref="File"/>
</logger>
</log4j:configuration>
PatternLayout in Log4j
The PatternLayout
Lets users define output formats using conversion patterns.
Output
2018-Sep-20 15:42:50,705 [main] com.log.LoggerExample
INFO Info method
2018-Sep-20 15:42:50,705 [main] com.log.LoggerExample
WARN Warn method
2018-Sep-20 15:42:50,705 [main] com.log.LoggerExample
ERROR Error method
2018-Sep-20 15:42:50,705 [main] com.log.LoggerExample
FATAL Fatal method
2018-Sep-20 15:42:54,675 [main] com.log.LoggerExample
DEBUG Debug method