In this tutorial, we will show you how to integrate the log4j framework with the Spring MVC web application.
All you need to do is :
- Include the
log4j.jar
as the project dependency. - Create a log4j.properties file and put it into the root of the classpath (for Maven, put it into the resources folder).
Technologies and tools used :
- Log4j 1.2.17
- Spring 4.0.5.RELEASE
- Maven 3
- Tomcat 6
- Eclipse Kepler 4.3
Note
By default, Spring is using the JCL dependency (commons-logging), and it has a runtime discovery algorithm to find out for other logging frameworks in well known places on the project classpath.
By default, Spring is using the JCL dependency (commons-logging), and it has a runtime discovery algorithm to find out for other logging frameworks in well known places on the project classpath.
1. Project Directory
Review the final project structure.

2. Project Dependencies
Declares the following dependencies :
pom.xml
<properties> <spring.version>4.0.5.RELEASE</spring.version> <log4j.version>1.2.17</log4j.version> <jstl.version>1.2</jstl.version> </properties> <dependencies> <!-- Spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- Log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- jstl , for JSP, optional --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> </dependencies>
3. log4j.properties
Create a log4j properties file, and put it into the resources folder, refer to step #1.
log4j.properties
# Root logger option log4j.rootLogger=DEBUG, stdout, file # Redirect log messages to console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # Redirect log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender #outputs to Tomcat home log4j.appender.file.File=${catalina.home}/logs/myapp.log log4j.appender.file.MaxFileSize=5MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
4. Spring MVC Controller + Message Logging
A simple controller to return a welcome page. Furthermore, it shows you how to use log4j to do the message logging.
WelcomeController.java
package com.mkyong.common.controller; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class WelcomeController { private static final Logger logger = Logger.getLogger(WelcomeController.class); @RequestMapping(value = "/", method = RequestMethod.GET) public ModelAndView getWelcome() { //logs debug message if(logger.isDebugEnabled()){ logger.debug("getWelcome is executed!"); } //logs exception logger.error("This is Error message", new Exception("Testing")); ModelAndView model = new ModelAndView("welcome"); model.addObject("msg", "Hello Spring MVC + Log4j"); return model; } }
5. Demo
Run the web application, and access the welcome controller.
5.1 All logging messages will be displayed in the console.
log4j.properties
# Redirect log messages to console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Eclipse console
Jul 03, 2014 7:19:31 PM org.apache.jk.server.JkMain start INFO: Jk running ID=0 time=0/11 config=null Jul 03, 2014 7:19:31 PM org.apache.catalina.startup.Catalina start INFO: Server startup in 1162 ms 2014-07-03 19:19:43 ERROR WelcomeController:23 - This is Error message java.lang.Exception: Testing at com.mkyong.common.controller.WelcomeController.getWelcome(WelcomeController.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source)
5.2 Furthermore, a logging file is created in the Tomcat’s logs folder.
log4j.properties
log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=${catalina.home}/logs/myapp.log log4j.appender.file.MaxFileSize=5MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
D:\apache-tomcat-6.0.37\logs\myapp.log
Jul 03, 2014 7:19:31 PM org.apache.jk.server.JkMain start INFO: Jk running ID=0 time=0/11 config=null Jul 03, 2014 7:19:31 PM org.apache.catalina.startup.Catalina start INFO: Server startup in 1162 ms 2014-07-03 19:19:43 ERROR WelcomeController:23 - This is Error message java.lang.Exception: Testing at com.mkyong.common.controller.WelcomeController.getWelcome(WelcomeController.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source)

Download Source Code
Download it –
Log4j-SpringMVC-Example.zip (11 KB)
References
- Spring Reference – Using log4j
- log4j 1.2 official page
- log4j hello world example
- log4j.properties examples
转:http://www.mkyong.com/spring-mvc/spring-mvc-log4j-integration-example/