log4j的日志记录:
所需jar包:
commons-logging.jar
log4j-1.2.15.jar
直接调用就可以:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
private Log logger = LogFactory.getLog(this.getClass());
public String returnUserInfo() {
logger.debug("返回UserInfo对象");
userInfo = new UserInfo();
userInfo.setUserId(10000);
userInfo.setUserName("张三");
userInfo.setPassword("000000");
return "userInfo";
}
需要明白一点的就是日志的级别,由低到高依次为:
debug<info<warn<error<fatal
最后在log4j.properties配置打印级别就ok了,下面参考sun公司在线宠物店的配置文件:
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J
log4j.rootLogger=INFO, stdout, logfile -- 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.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${petstore.root}/WEB-INF/petstore.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