南平市盟贝特志成网络技术有限公司
首页 | 联系方式 | 加入收藏 | 设为首页 | 手机站

产品目录

联系方式

联系人:业务部
电话: 00154-861715
邮箱:service@wyl19910906.com

当前位置:首页 >> 新闻中心 >> 正文

基于 maven 的 ssm 项目搭建四:整合 log4j

摘要:基于 maven 的 ssm 项目搭建四:整合 log4j

1、修改pom.xml

加入如下配置:
<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 <version>1.6.6</version>
</dependency>
<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-log4j12</artifactId>
 <version>1.6.6</version>
</dependency>
<dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.16</version>
</dependency>
修改后的pom.xml内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.whowii</groupId>
 <artifactId>website_java3</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>website_java3 Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <dependencies>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 <!-- spring -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>${springVersion}</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>${springVersion}</version>
 </dependency>
 <!-- log4j -->
 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 <version>1.6.6</version>
 </dependency>
 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-log4j12</artifactId>
 <version>1.6.6</version>
 </dependency>
 <dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.16</version>
 </dependency>
 </dependencies>
 <build>
 <finalName>website_java3</finalName>
 </build>
 <properties>
 <springVersion>4.3.8.RELEASE</springVersion>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
</project>

2、修改web.xml

加入log4j配置,修改后的web.xml内容如下:
<?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_3_0.xsd"
 version="3.0">
 <display-name></display-name>
 <context-param>
 <param-name>webAppRootKey</param-name>
 <!-- 同时部署多个站点时不允许相同,log4j中的相关配置项受该值影响 -->
 <param-value>whowii.website3</param-value>
 </context-param>
 
 <!-- log4j 配置 -->
 <context-param>
 <param-name>log4jConfigLocation</param-name>
 <param-value>classpath:log4j.properties</param-value>
 </context-param>
 <context-param>
 <param-name>log4jRefreshInterval</param-name>
 <param-value>60000</param-value>
 </context-param>
 <listener>
 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 </listener>
 
 <!-- spring mvc 配置 -->
 <servlet>
 <servlet-name>spring</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <!-- 配置spring servlet文件名称 -->
 <init-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>
 classpath:spring-servlet.xml
 </param-value>
 </init-param>
 <!-- load-on-startup:表示启动容器时初始化该Servlet -->
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>spring</servlet-name>
 <url-pattern>*.do</url-pattern>
 </servlet-mapping>
</web-app>

3、添加 log4j 配置文件

在 src/main/resources 目录下添加文件“log4j.properties”,文件内容如下:
###################
# Console Appender
###################
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target = System.out
log4j.appender.CONSOLE.Threshold = ERROR
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = \r\n\u7EA7\u522B\:%p\r\n\u4FE1\u606F\:%m\r\n\u4F4D\u7F6E\:%l\r\n\u65F6\u95F4\:%d\r\n\u7C7B\u76EE\:%c\r\n\u7EBF\u7A0B\:%t\r\n
########################
# DailyRolling File
########################
log4j.appender.FILE = org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File = ${whowii.website2}/WEB-INF/logs/syslog.log
log4j.appender.FILE.MaxBackupIndex = 5
log4j.appender.FILE.MaxFileSize = 2000KB
log4j.appender.FILE.Threshold = WARN
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH\:mm\:ss,SSS} method\:%l%n%m%n\r\n
log4j.logger.org.apache = INFO
log4j.rootLogger = DEBUG,CONSOLE,FILE

项目现有的结构如下:


4、添加测试代码

修改DemoController.java,完成的内容如下:
package com.whowii.core.manage.controller;
 
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class DemoController {
 private Logger logger = Logger.getLogger(getClass());
 
 @RequestMapping("/index")
 public String index(){
 logger.error(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
 return "demo";
 }
}


5、测试结果

打开浏览器,访问:http://localhost:8080/website_java3/index.do,
在MyEclipse的控制台窗口有输出如下:

在WEB-INF目录中自动生成了logs/syslog.log文件,内容如下:
[ERROR] 2017-06-08 11:48:11,266 method:com.whowii.core.manage.controller.DemoController.index(DemoController.java:13)

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>