Spring配置log4j实例

转自:http://www.vijun.com/web/spring/2012/03/5f84f4d935e5b13f01362de76085013d.html


Spring配置log4j实例首先需要在web.xml进行声明:


code
1
2
3
4
5
< context-param >
        < param-name >webAppRootKey param-name >
        < param-value >ssh.root param-value >
context-param >
这里的ssh是http://localhost:8080/ssh/

而root是系统开发目录中ssh/root

这样配置文件以及日志文件可以在spring的帮助下,放到开发环境中的任意位置
code
1
2
3
4
5
< context-param >
        < param-name >log4jConfigLocation param-name >
        < param-value >/WEB-INF/log4j.properties param-value >
context-param >
在这里定位配置文件,需要的是从root开始的绝对路径

code
1
2
3
4
5
6
7
8
9
10
< 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 >
设置监听器



/
之后我们就可以配置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

有了上面的配置,我们就可以查看日志了!

下面是我的

code
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.properties

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

你可能感兴趣的:(spring)