1.为什么使用日志?
日志可以记录项目中的重要信息,关键输出信息,异常信息,为项目上线后期维护提供方便,在项目开发中尽量养成习惯写日志,而不是System.out.println()打印,不过在junit测试类中可以。
2.选择哪个日志框架?
log4j.jar
3.日志的等级?
debug<info<warm<error,比如日志定位为info,只有级别比它高的才会输出。即优先权。
4.日志配置文件模板文件?
log4j.properties
log4j.rootCategory=INFO, stdout,logfile log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout #log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n log4j.appender.stdout.layout.ConversionPattern=%d %p [%c][%L] - <%m>%n log4j.appender.FILE.Append = false log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=INFO #log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n #log4j.appender.logfile.layout.ConversionPattern=%d %5p [%c] - <%m>%n log4j.appender.logfile.layout.ConversionPattern=%d %5p [%c][%L] - <%m>%n log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender #log4j.appender.logfile.File=${bncar.root}/WEB-INF/logs/app_log.log log4j.appender.logfile.File=E:/ws2/BNCAR/rel/WEB-INF/logs/app_log.log log4j.appender.logfile.DatePattern=.yyyy-MM-dd log4j.appender.logfile.layout=org.apache.log4j.PatternLayout #log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n log4j.appender.logfile.layout.ConversionPattern=%d %p [%c][%L] - <%m>%n log4j.appender.logfile.encoding=UTF-8
web.xml配置
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <context-param> <param-name>log4fRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
5.java代码中如何使用?
static Logger log = Logger.getLogger(DeleteCarBrandAction.class);
if(log.isInfoEnabled()){ log.info("message", e.fillInStackTrace()); }
log.error(e.getMessage()); //记录错误异常信息 Action层 ~~!!