1.下载jar包,使用的log4j版本是1.2.14
2.引包
3.增加配置文件,在src下创建文件,文件名为log4j.properties,复制以下模板粘贴:
# DEBUG < INFO < WARN < ERROR < FATAL
# Global logging configuration
log4j.rootLogger=debug, stdout,file
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{yyyy-MM-dd HH:mm:ss} %m%n
#file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=gtalent.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %m%n
4.引入文件进行测试
import org.apache.log4j.Logger;
public class Demo_log {
static Logger log = Logger.getLogger(Demo_log.class);
public static void main(String[] args) {
// TODO Auto-generated method stub
log.debug("记录而已");
}
}