spring集成日志log4j
准备工作:
开发工具:eclipse 或其他IDE
spring版本3.24下载地址:
Log4j版本:log4j-1.2.16.jar
1.创建一个动态web项目,名字任意指定
2.导入spring-framework-3.2.4.RELEASE\libs下以及log4j的jar包
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
log4j-1.2.16.jar
2.在classpath(也就是src下)下创建在log4j包的log4j.properties 具体配置说明可以百度一下
log4j.rootCategory=INFO,stdout,R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[springLogger] %d{yyyy MMM dd HH:mm:ss,SSS} %p [%t] %C.%M(%L) %m%n
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=${springLogger.root}/logs/logger.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
1log4j.appender.R.layout.ConversionPattern=[springLogger] %d{yyyy MMM dd HH:mm:ss,SSS} %p [%t] %C.%M(%L) %m%n
log4j.logger.org.springframework=DEBUG
log4j.logger.org.apache.velocity=FATAL在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" 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>springLogger</display-name>
<!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root"-->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>springLogger.root</param-value>
</context-param>
<!-- log4j.properties 的路径 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- 开一条watchdog线程每60秒扫描一下配置文件的变化 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<!-- log4j的日志监听-->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<!-- 在Spring2.0中除了以前的Singleton和Prototype外又加入了三个新的web作用域,分别为request、session和global session,如果你想让你的容器里的某个bean拥有其中某种新的web作用域,除了在bean级上配置相应的scope属性,还必须在容器级做一个额外的初始化配置 -->
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>在classpath(也就是src下)下创建 applicationContext.xml 资源文件,没有注入内容,只为测试
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
</beans>在项目中com.test下创建一个Main.java类文件
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Assert;
/**
* @author HL Yang
* @date 创建时间:2014年11月25日 下午4:09:30
*/
public class Main {
public static void main(String args[]) throws InterruptedException{
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Assert.isNull(ctx,"this ctx is not null ");
}
}至此配置文件都已完成
需要注意的两点是:
<!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root"-->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>springLogger.root</param-value>
</context-param>
可以与log4j.properties中log4j.appender.R.File=${springLogger.root}/logs/logger.log对应输出