log4j是很常用的日志类包,在此做一下配置的记录
- 加载jar包和properities配置文件
将log4j.properties放在src根目录中或者resource中。
- properties具体配置信息
log4j.rootLogger = INFO , info, warn, error, stdout ###控制台输出 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 = %d{ABSOLUTE} %5p %c{1}:%L - %m%n ###info级别输出 log4j.logger.info = info log4j.appender.info = org.apache.log4j.DailyRollingFileAppender log4j.appender.info.File = ${webroot}/logs/convert_info.log log4j.appender.info.Append = true log4j.appender.info.Threshold = INFO log4j.appender.info.layout = org.apache.log4j.PatternLayout log4j.appender.info.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n log4j.appender.info.datePattern='.'yyyy-MM-dd log4j.appender.info.filter.infoFilter = org.apache.log4j.varia.LevelRangeFilter log4j.appender.info.filter.infoFilter.LevelMin=INFO log4j.appender.info.filter.infoFilter.LevelMax=INFO ###warn级别输出 log4j.logger.warn = warn log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender log4j.appender.warn.File = ${webroot}/logs/convert_warn.log log4j.appender.warn.Append=true log4j.appender.warn.Threshold=WARN log4j.appender.warn.layout=org.apache.log4j.PatternLayout log4j.appender.warn.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n log4j.appender.warn.datePattern='.'yyyy-MM-dd log4j.appender.warn.filter.warnFilter=org.apache.log4j.varia.LevelRangeFilter log4j.appender.warn.filter.warnFilter.LevelMin=WARN log4j.appender.warn.filter.warnFilter.LevelMax=WARN ###error级别输出 log4j.logger.error = error log4j.appender.error = org.apache.log4j.DailyRollingFileAppender log4j.appender.error.File = ${webroot}/logs/convert_error.log log4j.appender.error.Append = true log4j.appender.error.Threshold = ERROR log4j.appender.error.layout = org.apache.log4j.PatternLayout log4j.appender.error.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n log4j.appender.error.datePattern='.'yyyy-MM-dd log4j.appender.error.filter.errorFilter=org.apache.log4j.varia.LevelRangeFilter log4j.appender.error.filter.errorFilter.LevelMin=ERROR log4j.appender.error.filter.errorFilter.LevelMax=ERROR
- 给webroot变量设置值
private void log4jInitialized(){ String log4jPath = ""; try { log4jPath = ApplicationInitListener.class.getResource("").toURI().getPath(); log4jPath = log4jPath.substring(0, log4jPath.indexOf("WEB-INF")) + "WEB-INF"; } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.setProperty("webroot", log4jPath); }
在网站应用程序初始化时调用
public void contextInitialized(ServletContextEvent arg0) { /*初始化日志路径*/ log4jInitialized(); }
- 参考资料
1、网上很多一样号称能够实现分级存储日志的配置,但实际上不是真正的分级存储
http://www.iteye.com/topic/744693
2、真正能够实现分级的而不需要实现编程的配置方法
http://wendal.net/219.html
3、通过编程方式实现分级方法,有了上面的实现方式,不用进行编程,哈
http://blog.csdn.net/projava/article/details/7344839
来源:https://www.cnblogs.com/wala-wo/p/5119240.html