首页 > 代码库 > 在Spring项目中使用Log4j记录日志
在Spring项目中使用Log4j记录日志
(1)引入log4j的jar包:
官网下载地址:http://logging.apache.org/log4j/1.2/download.html
(2)在web.xml中添加log4j配置:
1
2
3
4
5
6
7
8
|
< 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 > |
(3)log4j属性文件log4j.properties:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#Root Category log4j.rootLogger=debug,stdout,D,E #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 = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n #DEBUG log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = ${catalina.base}/logs/debug.log log4j.appender.D.Append = true log4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n #ERROR log4j.appender.E = org.apache.log4j.DailyRollingFileAppender log4j.appender.E.File =${catalina.base}/logs/error.log log4j.appender.E.Append = true log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n #IBATIS Category log4j.logger.com.ibatis=DEBUG log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG #WS Logger for reporting log4j.logger.WS=INFO,WS log4j.appender.WS=org.apache.log4j.DailyRollingFileAppender log4j.appender.WS.File=${catalina.base}/logs/ws.log log4j.appender.WS.layout=org.apache.log4j.PatternLayout log4j.appender.WS.DatePattern =‘.‘yyyy-MM-dd log4j.appender.WS.layout.ConversionPattern=%d-[ws] %p %t %c - %m%n #Other Logger log4j.logger.org.springframework=INFO log4j.logger.com.ibatis.db=INFO log4j.logger.net.sf.json=ERROR log4j.logger.org.apache.cxf=DEBUG,WS |
注:我这里的配置文件并不是很好,仅供参考
(4)测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package cn.zifangsky.controller; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController { private static Logger logger = Logger.getLogger(TestController. class ); @RequestMapping ( "/test.html" ) public void test(){ logger.info( "info测试" ); logger.debug( "debug测试 " ); logger.error( "error测试" ); } } |
控制台输出如下:
1
2
3
4
5
6
|
[INFO ] 2016-11-06 18:01:28,000 method:cn.zifangsky.controller.TestController.test(TestController.java:13) info测试 [DEBUG] 2016-11-06 18:01:28,007 method:cn.zifangsky.controller.TestController.test(TestController.java:14) debug测试 [ERROR] 2016-11-06 18:01:28,007 method:cn.zifangsky.controller.TestController.test(TestController.java:15) error测试 |
debug.log文件:
1
2
3
|
2016-11-06 18:01:28 [ http-apr-9180-exec-10:0 ] - [ INFO ] info测试 2016-11-06 18:01:28 [ http-apr-9180-exec-10:7 ] - [ DEBUG ] debug测试 2016-11-06 18:01:28 [ http-apr-9180-exec-10:7 ] - [ ERROR ] error测试 |
error.log文件:
1
|
2016-11-06 18:01:28 [ http-apr-9180-exec-10:7 ] - [ ERROR ] error测试 |
在Spring项目中使用Log4j记录日志
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。