Log4j 2 + Spring MVC Integration

Log4j 2 provides simple solution to the integration of logging to the web applications. If you run servlet 3.0 web application, then there is no need for any configurations. The log4j context is initialized at the time of loading the web application. If you run the servlet 2.5 web application, there is configuration parameters to be added in the web.xml deployment descriptor to enable the logging. This tutorial explain the simple integration of Log4j 2 and Spring integration.

Note that, log4j 2 doesn’t support the servlet 2.4 and prior web applications. This tutorial explains how to integrate log4j 2 with the simple Spring MVC application.

HelloController.java

package javabeat.net.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.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("/welcome")
public class HelloController {
	static final Logger logger = LogManager.getLogger(HelloController.class.getName());
	@RequestMapping(method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {
		logger.info("Spring MVC Log4j!");
		model.addAttribute("message", "Spring 3 MVC Hello World");
		return "hello";

	}

}

log4j2.java

Add the log4j2.xml in the classpath. To make it simple, add this file to the root of your source code package.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
	<Appenders>
		<Console name="CONSOLE" target="SYSTEM_OUT">
			<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
		</Console>
	</Appenders>
	<Loggers>
		<logger name="javabeat.net.controller" level="INFO" />
	</Loggers>
</Configuration>

hello.jsp

<html>
<body>
	Message : ${message}
</body>
</html>

springmvc-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="

http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="javabeat.net.controller" />

	<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>

</beans>

web.xml

In the deployment descriptor, you have to add listener and filter to enable the logging.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>SpringMVC</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<listener>
		<listener-class>org.apache.logging.log4j.core.web.Log4jServletContextListener</listener-class>
	</listener>

	<filter>
		<filter-name>log4jServletFilter</filter-name>
		<filter-class>org.apache.logging.log4j.core.web.Log4jServletFilter</filter-class>
	</filter>
 <filter-mapping>
        <filter-name>log4jServletFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

Output…

If you run the above program, you would get the output message as below.

16:44:19.732 [http-8080-1] INFO  javabeat.net.controller.HelloController - Spring MVC Log4j!

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*