您的位置:首页 > 其它

log4j 实现日志管理

2015-05-19 16:17 405 查看
之前虽然接触过log4j 但是一直不知道怎么一回事,只知道这个log4j.jar是进行日志操作的,jar包可以去这里下载:jarfire (https://cn.jarfire.org/)

最近学习SpringMVC 看资料又遇到了log4j ,感觉这个在今后的开发中应该很有用,所以就想好好学习下。

首先先讲讲如何在工程中配置吧:

1.把log4j.jar 拷贝到你所建工程的 WEB_INF/lib下

2.在工程的 src目录下新建一个 log4j.properties 文件,用来配置日志文件的一些属性:

内容可以如下:

log4j.rootLogger=DEBUG, RC
log4j.appender.RC=org.apache.log4j.ConsoleAppender
log4j.appender.RC.layout=org.apache.log4j.PatternLayout
log4j.appender.RC.immediateFlush=true
log4j.appender.RC=org.apache.log4j.RollingFileAppender
log4j.appender.RC.File=mylog.logs
log4j.appender.RC.MaxFileSize=100KB
log4j.appender.RC.MaxBackupIndex=1
log4j.appender.RC.layout=org.apache.log4j.PatternLayout
log4j.appender.RC.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p :%m;%n


这种配置运行后,会在工程的当前目录下新建一个 mylog.logs 的文件用来保存日志

日志内容样式如下:

2015-05-19 16:25:46,243 DEBUG :Initializing new StandardEnvironment;
2015-05-19 16:25:46,243 DEBUG :Adding [systemProperties] PropertySource with lowest search precedence;
2015-05-19 16:25:46,244 DEBUG :Adding [systemEnvironment] PropertySource with lowest search precedence;
2015-05-19 16:25:46,244 DEBUG :Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment];
2015-05-19 16:25:46,245 DEBUG :Initializing new StandardEnvironment;
2015-05-19 16:25:46,245 DEBUG :Adding [systemProperties] PropertySource with lowest search precedence;
2015-05-19 16:25:46,245 DEBUG :Adding [systemEnvironment] PropertySource with lowest search precedence;
2015-05-19 16:25:46,246 DEBUG :Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment];
2015-05-19 16:25:46,247 DEBUG :Initializing new StandardEnvironment;
2015-05-19 16:25:46,247 DEBUG :Adding [systemProperties] PropertySource with lowest search precedence;
2015-05-19 16:25:46,247 DEBUG :Adding [systemEnvironment] PropertySource with lowest search precedence;
2015-05-19 16:25:46,248 DEBUG :Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment];
2015-05-19 16:25:46,249 DEBUG :Initializing new StandardEnvironment;
2015-05-19 16:25:46,249 DEBUG :Adding [systemProperties] PropertySource with lowest search precedence;
2015-05-19 16:25:46,250 DEBUG :Adding [systemEnvironment] PropertySource with lowest search precedence;
2015-05-19 16:25:46,250 DEBUG :Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment];
2015-05-19 16:25:46,250 DEBUG :Converting String to [double] using property editor [org.springframework.beans.propertyeditors.CustomNumberEditor@693ec1db];
2015-05-19 16:25:46,250 DEBUG :Finished creating instance of bean 'yourBook';


你也可以用下面这种方式配置你的log4j.propertiess:
内容如下:

log4j.rootLogger=INFO, RC
log4j.appender.RC=org.apache.log4j.ConsoleAppender
log4j.appender.RC.threshold=debug
log4j.appender.RC.layout=org.apache.log4j.PatternLayout
log4j.appender.RC.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p :%m;%n
log4j.appender.RC.immediateFlush=true
log4j.logger.org.springframework=DEBUG


这样你的内容就会在控制台窗口输出。



有时间再补充详细介绍:参考网站:http://blog.csdn.net/edward0830ly/article/details/8250412
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: