(1)引入log4j的jar包:
官网下载地址:http://logging.apache.org/log4j/1.2/download.html
(2)在web.xml中添加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>
(3)log4j属性文件log4j.properties:
#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)测试:
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测试"); } }
控制台输出如下:
[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文件:
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文件:
2016-11-06 18:01:28 [ http-apr-9180-exec-10:7 ] - [ ERROR ] error测试
参考文章:
http://blog.csdn.net/anlina_1984/article/details/5313023
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。