Integrating Log4j with Spring 3.0 MVC

Here is an explanation of log4j integration with Spring 3.0 MVC Project.

Server – apache-tomcat-7.0.34
Log4j Version – log4j-1.2.16.jar

What is log4j?

Open Source java based framework of Apache Foundation.
It helps to output log statements to output targets.

Components of Log4j?

  • loggers: Responsible for capturing logging information.
  • appenders: Responsible for publishing logging information to various preferred destinations.
  • layouts: Responsible to format logging information in different styles.

There are various different log levels

ALL -All levels including custom levels.

DEBUG-Used to indicate events that are useful to debug an application. Handling method for DEBUG level is: debug().

INFO-Used to highlight the progress of the application. Handling method for INFO level is: info().

WARN-Used to indicate potentially harmful situations. Handling method for WARN level is: warn().

ERROR-Shows errors messages that might not be serious enough and allow the application to continue. Handling method for ERROR level is: error().

FATAL– Used toindicate severe events that will may cause abortion of the application. Handling method for FATAL level is: fatal().

OFF -The highest possible rank and is intended to turn off logging.

How to Integrate log4j into the Spring 3.0 Project?

1. Add log4j jar file into classpath.

2. Create log4j’s configuration in the Project.

Create log4j’s configuration.

Log4j configuration file can be property file or xml file. Xml file has added advantatages than property file. Eg: Filters, custom ErrorHandlers and a special type of appenders, i.e. AsyncAppender. ErrorHandlers define

i. Add the following entry to web.xml file



ii. Place the log4j.xml file inside /WEB-INF folder

Following is sample log4j.xml file

<!--?xml version="1.0" encoding="UTF-8"?-->

<?xml version="1.0" encoding="UTF-8"?>

<log4j:configuration xmlns:log4j="">

	<appender name="fileAppender" class="org.apache.log4j.FileAppender">
		<param name="File" value="${catalina.home}/logs/LogFile.log" />
		<param name="MaxFileSize" value="5MB" />
		<param name="MaxBackupIndex" value="50" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%-7p %d [%t] %c %x - %m%n" />


	<appender name="htmlFileAppender" class="org.apache.log4j.FileAppender">
		<param name="File" value="${catalina.home}/logs/LogFile.html" />
		<param name="MaxFileSize" value="5MB" />
		<param name="MaxBackupIndex" value="50" />
		<layout class="org.apache.log4j.HTMLLayout">
			<param name="ConversionPattern" value="%-7p %d [%t] %c %x - %m%n" />


	<appender name="stdOut" class="org.apache.log4j.ConsoleAppender">
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%-7p %d [%t] %c %x - %m%n" />

	<!-- package level log levels can be define -->
	<category name="com.res.common.controller">
		<priority value="debug" />

	<!-- Root Logger trace/debug/info/warn/error/fatal/off> -->
		<priority value="info" />
		<appender-ref ref="fileAppender" />
		<appender-ref ref="htmlFileAppender" />
		<!-- remove stdOut to disable printing logs in server console -->
		<appender-ref ref="stdOut" />



Following things are included in the sample log4j.xml file

  • Two types of appenders are in the sample log4j.xml. Eg : FileAppender and CosoleAppender.
  • Package level log levels definition is defined
  • Log file layout both text and html formats
  • Root logger is defined
    The root logger is the base logger from which other loggers inherits. Syntax for configuring the root logger is as follows:

log4j.rootLogger=level[, appender1, appender2…]

Java Code Example for adding loggers

import org.apache.log4j.Logger;
public class Test {

    private static final Logger logger = Logger.getLogger(Test.class);

    Test() {


    public void methodOne() {"Add info message");
        logger.warn("Add warn message");


Tagged with: , , , , , , ,
Posted in Uncategorized
3 comments on “Integrating Log4j with Spring 3.0 MVC
  1. micheal says:

    good article, but tell me what are the changes should i do if i use JBOSS instead tomcat, please urgent

  2. java2novice says:

    very nice. for more java examples, visit site

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: