转自:http://www.vijun.com/web/spring/2012/03/5f84f4d935e5b13f01362de76085013d.html
Spring配置log4j实例首先需要在web.xml进行声明:
1
2
3
4
5
|
<!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root"-->
<
context-param
>
<
param-name
>webAppRootKey</
param-name
>
<
param-value
>ssh.root</
param-value
>
</
context-param
>
|
而root是系统开发目录中ssh/root
这样配置文件以及日志文件可以在spring的帮助下,放到开发环境中的任意位置
1
2
3
4
5
|
<!--由Sprng载入的Log4j配置文件位置-->
<
context-param
>
<
param-name
>log4jConfigLocation</
param-name
>
<
param-value
>/WEB-INF/log4j.properties</
param-value
>
</
context-param
>
|
1
2
3
4
5
6
7
8
9
10
|
<!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond-->
<
context-param
>
<
param-name
>log4jRefreshInterval</
param-name
>
<
param-value
>60000</
param-value
>
</
context-param
>
<!--Spring log4j Config loader-->
<
listener
>
<
listener-class
>org.springframework.web.util.Log4jConfigListener</
listener-class
>
</
listener
>
|
/
之后我们就可以配置log4j配置文件了
#先设置级别
log4j.rootCategory=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#在这里设置日志需要存放的位置,这里的变量就是我们在web.xml里设置的
log4j.appender.file.File=${ssh.root}/WEB-INF/logs/subject.log
log4j.appender.file.MaxFileSize=100KB
log4j.appender.file.MaxBackupIndex=0
log4j.appender.file.layout=org.apache.log4j.SimpleLayout
log4j.appender.file.layout.ConversionPattern=[ssh] %p %t %c - %m%n
有了上面的配置,我们就可以查看日志了!
下面是我的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
web.xml
<
context-param
>
<
param-name
>webAppRootKey</
param-name
>
<
param-value
>PreOfhibernate.WebRoot</
param-value
>
</
context-param
>
<
context-param
>
<
param-name
>log4jConfigLocation</
param-name
>
<
param-value
>/WEB-INF/log4j.properties</
param-value
>
</
context-param
>
<
context-param
>
<
param-name
>log4jRefreshInterval</
param-name
>
<
param-value
>60000</
param-value
>
</
context-param
>
|
log4j.rootLogger=warn,eimhec,eimhef
log4j.appender.eimhec=org.apache.log4j.ConsoleAppender
log4j.appender.eimhef=org.apache.log4j.RollingFileAppender
log4j.appender.eimhef.File=eimhees.log
log4j.appender.eimhec.layout=org.apache.log4j.SimpleLayout
log4j.appender.eimhef.layout=org.apache.log4j.PatternLayout
log4j.appender..eimhef.layout.ConversionPattern=%t %p - %m%n
commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger