今天写了一个log4j的配置,给以后的自己看
第一步:
导入log4j的jar包
第二步:
在src下面建一个log4j.properties文件,在文件里写好配置,如:
log4j.rootLogger=DEBUG,stdout
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{yy/MM/dd HH:mm:ss:SSS}][%C-%M] %m%n
#log4j.appender.logfile=org.apache.log4j.RollingFileAppender
#log4j.appender.logfile.File=$/logs/webapp.log
#log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
#log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
#log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
#log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
#log4j.logger.com.opensymphony.xwork2=ERROR
# Control logging for other open source packages
#log4j.logger.org.springframework=ERROR
#log4j.logger.org.quartz=ERROR
#log4j.logger.net.sf.ehcache=ERROR
#log4j.logger.net.sf.navigator=ERROR
#log4j.logger.org.apache.commons=ERROR
#log4j.logger.org.apache.struts=ERROR
# Struts OgnlUtil issues unimportant warnings
#log4j.logger.com.opensymphony.xwork2.util.OgnlUtil=error
#log4j.logger.com.opensymphony.xwork2.ognl.OgnlValueStack=error
第三步:
在web.xml中配置要加载的log4j.properties文件,如下:
<!-- 配置log4j -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/config/log4j.properties</param-value>
</context-param>
<listener>
<!-- 配置log4j -->
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
第四步:
在你要打印日志的类里加入日志,如下:
/**
* 日志
*/
private static Logger logger = Logger.getLogger(UserDAOImpl.class);
logger.debug(sql);
logger.debug(user.getName());
logger.info(user.getPasswd());