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
<properties> <spring.version>3.0.5.RELEASE</spring.version> </properties> <dependencies> <!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </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.
HelloWorldController.java
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{ @Override 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
${message}
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" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean>
4. Integrate Spring in Web App
To integrate Spring in Web Application, write the following code in web.xml
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
5. Properties file
Create log4j.properties file that defines standard rules required for Log4J to handle logging messages.
log4j.properties
log4j.rootLogger=DEBUG, CA log4j.appender.CA=org.apache.log4j.ConsoleAppender log4j.appender.CA.layout=org.apache.log4j.PatternLayout 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; @Controller @RequestMapping("/helloworld.html") 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!!!