Spring MVC整合 Log4j
環境及工具:
Log4j 2.6.2
Spring 4.3.2.RELEASE
Maven 3
Tomcat 8(idea中tomcat配,參考:http://blog.csdn.net/jiankunking/article/details/51921092)
IntelliJ IDEA 15.0.6(開啟maven自動匯入)
預設情況下,Spring(spring-core)是使用JCL(commons-logging)日誌記錄、JCL執行時能通過發現演算法在專案類路徑下找出其他日誌框架。
為了整合log4j你需要做以下幾個事情:
1、將log4j.jar放到專案類路徑下
2、建立log4j.properties或者 log4j.xml在專案根類路徑下(如果你遵循Maven標準目錄結構,該檔案應該放在resources 資料夾下 )
1. 專案目錄
以下是最終的專案結構:
2、專案依賴
<properties>
<spring.version>4.3.2.RELEASE</spring.version>
<log4j.version>2.6.2</log4j.version>
</properties>
<dependencies>
<!-- Spring -->
<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>
</dependencies>
3. log4j.properties
建立log4j.properties檔案,並把它放到resources資料夾下,參考上面的專案目錄結構。
# 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
一個簡單的controller 返回一個welcome頁面,該controller將展示如何用log4j 記日誌。
package com.jiankunking.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、原始碼下載
http://download.csdn.net/detail/xunzaosiyecao/9602083
本文參考:https://www.mkyong.com/spring-mvc/spring-mvc-log4j-integration-example/#download
作者:jiankunking 出處:http://blog.csdn.net/jiankunking
相關文章
- Spring整合log4jSpring
- Spring MVC整合redis的配置SpringMVCRedis
- Shiro和Spring MVC、Mybatis整合教程SpringMVCMyBatis
- Spring、Spring MVC、MyBatis 整合檔案配置詳解SpringMVCMyBatis
- Spring、Spring MVC、MyBatis整合檔案配置詳解SpringMVCMyBatis
- Spring MVC 整合 Shiro 許可權控制SpringMVC
- Eclipse下整合spring mvc 配置檔案EclipseSpringMVC
- Spring MVC學習筆記和SSH的整合SpringMVC筆記
- Spring配置log4jSpring
- Spring MVCSpringMVC
- spring - mvcSpringMVC
- Spring MVC 零配置 / Spring MVC JavaConfigSpringMVCJava
- Spring Boot/Spring MVCSpring BootMVC
- spring、spring MVC、spring BootMVCSpring Boot
- spring mvc dubbo ios android整合cms內容釋出平臺SpringMVCiOSAndroid
- spring mvc+mybatis整合 cms 實現UC瀏覽器文章功能SpringMVCMyBatis瀏覽器
- spring mvc interceptorsSpringMVC
- spring - mvc - @ScheduledSpringMVC
- 在Spring中配置log4jSpring
- Spring Boot + Mybatis + Spring MVC環境配置(四):MVC框架搭建Spring BootMyBatisMVC框架
- Spring MVC 面試題SpringMVC面試題
- Spring MVC應用SpringMVC
- Spring5 MVCSpringMVC
- Spring MVC 簡述SpringMVC
- Spring MVC DispatcherServlet 配置SpringMVCServlet
- Spring MVC 完整示例SpringMVC
- Spring MVC 流程圖SpringMVC流程圖
- Spring自定義MVCSpringMVC
- spring - mvc - @ValidSpringMVC
- Spring MVC基礎SpringMVC
- Spring MVC學習SpringMVC
- Mybatis、maven專案中整合log4j (17)MyBatisMaven
- Spring mvc+struts2+Hibernate整合例項程式碼(註解詳解)SpringMVC
- spring cloud多模組專案框架搭建-整合SLF4J和log4j日誌元件SpringCloud框架元件
- tomcat + spring mvc原理外傳:spring mvc與前端的糾葛TomcatSpringMVC前端
- 跟開濤學Spring MVC:第二章 Spring MVC入門SpringMVC
- spring mvc即mvc攔截器例項(1)SpringMVC
- Spring MVC for beginners 筆記SpringMVC筆記