Maven加两个依赖
http://mvnrepository.com/artifact/org.slf4j/slf4j-api
http://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12
src/main/resources/ 放入 log4j.properties
内容如下:
log4j.rootLogger=INFO,stdout,R
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,SSS} [%t] [%c] [%p] - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=spider-job3.log
log4j.appender.R.MaxFileSize=500KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n
log4j.rootLogger=INFO,stdout,R
生产中一般用info或者error,测试用debug
可以设置级别: debug>info>error
Logger logger = LoggerFactory.getLogger(getClass());
logger.error("现在时间是{}",System.currentTimeMillis());