Spring + Log4j + Maven Example

In this tutorial, I will show you how to integrate Spring 3 with Log4j.

Tools and technologies used :

  • Spring
  • Log4j
  • Maven
  • Eclipse

Following example demonstrates a simple Hello World web application using Spring MVC that will show logging message on Console.

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml



		<!-- Spring 3 dependencies -->



1. Controller

To provide a basic infrastructure, all of Spring’s various Controller inherit from AbstractController. When using the AbstractController as the baseclass for your controllers you only have to override the handleRequestInternal(HttpServletRequest, HttpServletResponse) method, implement your logic, and return a ModelAndView object.


package com.kruders.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloWorldController extends AbstractController{
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		ModelAndView model = new ModelAndView("helloworld");
		model.addObject("message", "Hello World!!!");
		return model;

2. Create View

Create jsp folder in WEB-INF and create helloworld.jsp file in jsp folder

Now write the following code in helloworld.jsp file


3. Configuration

Create Spring Bean Configuration in WEB-INF folder and name it dispatcher-servlet.xml and add the following code.

<bean name="/helloworld.html" class="com.kruders.controller.HelloWorldController" />
    <property name="prefix">
    <property name="suffix">

4. Integrate Spring in Web App

To integrate Spring in Web Application, write the following code in web.xml


5. Properties file

Create log4j.properties file that defines standard rules required for Log4J to handle logging messages.


log4j.rootLogger=DEBUG, CA


log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n 

6. Integrate Log4j in Spring MVC

Create a Class variable for Logger as shown below:

package com.kruders.controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public class HelloWorldController {
	static final Logger logger = Logger.getLogger(HelloWorldController.class);
	@RequestMapping(method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {
		logger.info("Hello World!!!");
		model.addAttribute("message", "Hello World!!!");
		return "helloworld";


When you run the above example you’ll get an output (on console) like:

1531 [http-8181-2] INFO com.kruders.controller.HelloWorldController – Hello World!!!

, ,

2 Responses to Spring + Log4j + Maven Example

  1. Tim January 18, 2013 at 2:40 am #

    I think logback is best practice these day, no?

  2. bagdagol September 12, 2013 at 8:19 am #

    hi please send me a executable code of this application

Leave a Reply