Log4j Example using XML configuration

In this tutorials we are going to see how to create simple log4j application using XML file Configuration with java program, previously we have seen log4j application using Property Configurator we can obtain same output using XML configuration file.

Add Log4j jar in your class path or Log4j maven dependency in your pom.xml . This tutorial created based on log4j 1.2.17 version. Check our latest tutorials to know about Log4j 2 .

Log4j Maven dependency

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

log4j.properties

log4j.rootLogger= Debug,console

log4j.appender.console=org.apache.log4j.ConsoleAppender

log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%r [%t] %p %c %x - %m%n

log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
 <appender name="console" class="org.apache.log4j.ConsoleAppender">
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%r [%t] %p %c %x - %m%n" />
  </layout>
 </appender>
 <root>
  <level value="DEBUG" />
  <appender-ref ref="console" />
 </root>
</log4j:configuration>

This Example work same as previous one. We used DOMConfigurator.configure() to load xml configuration.root tag used to configure Level and Appender, in this example I used DEBUG as Level and Console appender as appender(console is name of appender).Layout is used to define log message layout pattern which is configured inside appender tag PatternLayout is used to configure log4j message conversion pattern.We will see Pattern Layout in detail in upcoming chapter.

Log4jXMLExample.java

package com.ehowtonow.log4j.xml;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class Log4jXMLExample {

 private static final String FILE_PATH = "log4j.xml";
 private static final Logger log = Logger.getLogger(Log4jXMLExample.class);

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  if (FILE_PATH == null) {
   BasicConfigurator.configure();
   log.info("Log4j Configured using Basic Configurator");
  } else {
   DOMConfigurator.configure(FILE_PATH);
   log.info("Log4j Configured using DOMConfigurator Configurator");
  }

  log.debug("Debug message by ehowtonow.com");
  log.info("Info message by ehowtonow.com");
  log.warn("Warn message by ehowtonow.com");
  log.error("Error message by ehowtonow.com");
  log.fatal("Fatal message by ehowtonow.com");

 }

}

Create instance for Logger class , getLogger(Log4jXMLExample.class) method accept fully qualified class name as its argument. In this example if xml file not exist in specified location then BasicConfigurator.configure() used to initialize log4j application otherwise DOMConfigurator.configure(FILE_PATH) loads the configuration from xml file .

now you can run the application, you will see the following output in console.

0 [main] INFO com.ehowtonow.log4j.xml.Log4jXMLExample – Log4j Configured using DOMConfigurator Configurator
0 [main] DEBUG com.ehowtonow.log4j.xml.Log4jXMLExample – Debug message by ehowtonow.com
0 [main] INFO com.ehowtonow.log4j.xml.Log4jXMLExample – Info message by ehowtonow.com
0 [main] WARN com.ehowtonow.log4j.xml.Log4jXMLExample – Warn message by ehowtonow.com
0 [main] ERROR com.ehowtonow.log4j.xml.Log4jXMLExample – Error message by ehowtonow.com
0 [main] FATAL com.ehowtonow.log4j.xml.Log4jXMLExample – Fatal message by ehowtonow.com


Posts You may Like

  1. Java Code Conventions – Java Best Practice
  2. How to Convert Object to JSON String using GSON
  3. How to find parent directory of file using Java
  4. How to get Character unicode point in a String
  5. How to Create File using Java
  6. How to Convert JSON String to Object using GSON
  7. How to Modify XML file using DOM Parser
  8. Lambda expressions are allowed only at source level 1.8 or above
  9. Sending HTML Email using Java Mail API
  10. How to Write File using BufferedWriter
  11. First Java Application in Eclipse
  12. How to search file from directory using Java
  13. GSON Introduction
  14. How to validate IPv4 Address using Java Regular Expression
  15. How to reverse the Collection (List)

Be the first to comment

Leave a Reply

Your email address will not be published.


*