Log4j Configuration

Log4j Configuration

Log4j Configuration File includes all Log4j configuration details like rootLogger, Layout configurations, Appenders configuration etc.

In Log4j , we are able to use the following two types of configuration files.

  1. log4j.properties
  2. log4j.xml

In both the configuration files, we have to use the following configurations at basic java applications.

  1. Declare rootLogger and initialize rootLogger
  2. Declare appender
  3. Declare Layout and its conversionPattern.
log4j.properties
log4j.rootLogger =TRACE, CONSOLE
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%d{yyyy-mm-dd hh:mm:ss } : This is %m%n

Where

  • Where “log4j.rootLogger” property will take Logging LEVEL and appender name
  • Where Log4j framework will display all the messages which are same as the specified Log level and lower than the specified Log level.
  • Where appender name is a name which is referring a particular Appender.

E.g

log4j.rootLogger = TRACE, CONSOLE

Where “log4j.appender.CONSOLE” property will take a particular Appender like ConsoleAppender, FileAppender etc

E.g

log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender

Where “log4j.appender.CONSOLE.layout” property will configure Layout configuration.

E.g

log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout

Where PattrenLayout needs a property like conversionPattern to take a particular pattern to display messages.

E.g

log4j.appender.CONSOLE.layout.conversionPattern = %m%n

Note

Where %m%n in the sense, %m is to display message and %n is to bring cursor to next line.

log4j.xml Configuration

In XML confioguration we have to use the following configuration.

  1. Appender configuration.
  2. Layout Configuration
  3. Root Logger Configuration

To provide XML configuration then we have to use the following xml tags in log4j.xml file.

log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
   <appender name="console" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{DD/MM/YYYY HH:mm:SS} %- 5p %c{1} - %m%n" />
      </layout>
   </appender>
   <root>
      <priority value="ERROR" />
      <appender-ref ref="console" />
   </root>
</log4j:configuration>
  • Where <log4j:configuration> is a root tag, it able to take log4j configuration details.
  • Where tag is able to configure Appender class with a logical name.
  • Where “name” attribute in tag will take logical name to the appender.
  • Where “class” attribute in tag is able to take fully qualified name of the Appender class, that is, “org.apache.log4j.ConsoleAppender”
  • Where tag can be used to configure Layout class.
  • Where “class” attribute in tag will take fully qualified name of Layout class, that is “org.apache.log4j.PatternLayout”.
  • Where tag in tag will take layout parameter .
  • Where “name” attribute will take parameter name, that is , “ConversionPattern”.
  • Where “value” attribute will take value of the parameter, that is, %d{DD/MM/YYYY HH:mm:SS} %-5p %c{1} – %m%n
  • Where tag will provide rootLogger configuration.
  • Where tag tag will take logger level with “value” attribute.
  • Where tag will take logical name of the appender with “ref” attribute.
Steps to Use Log4j in Java Application
  • Download log4j-1.2.17.zip from https://logging.apache.org/log4/1.2/download.html
  • Create Java project in Eclipse IDE and add log4j1.2.17.jar file in build path
  • Create log4j configuration file[log4j.properties or log4j.xml].
  • Create Java class.
  • Run Java project [Suggestible in Debug mode].

E.g

package com.ashok.log4j;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class TestLog4J {
	private static final Logger LOG = LoggerFactory.getLogger(TestLog4J.class);
	public static void main(String[] args) {
		LOG.trace("This is trace message"); 
		LOG.debug("This is debug message");
		LOG.info("This is info message"); 
		LOG.warn("This is warn message"); 
		LOG.error("This is error message"); 
	}
}

If we want to use XML configuration then we have to use the following XML file otherwise you can use log4j.properties file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
   <appender name="console" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{DD/MM/YYYY HH:mm:SS} %- 5p %c{1} - %m%n" />
      </layout>
   </appender>
   <root>
      <priority value="ERROR" />
      <appender-ref ref="console" />
   </root>
</log4j:configuration>

Log4j Configuration
Scroll to top