步骤:
1、导入jar包:下载log4j-1.2.17.jar,导入项目
2、写配置文件(只介绍一种):在src文件目录下新建文件,命名为log4j.properties配置文件,内容为:
log4j.rootLogger = trace,CONSOLE,UserLog
log4j.logger.me=DEBUG
### \u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target = System.out
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L [%t:%r]- %m%n
### \u8F93\u51FA\u5230\u65E5\u5FD7\u6587\u4EF6 ###
log4j.appender.UserLog = org.apache.log4j.DailyRollingFileAppender
log4j.appender.UserLog.File =D\:\\var\\alldata\\User\\debug.log
#log4j.appender.zhangsanLog.File =/var/alldata/zhenduan/debug.log
log4j.appender.UserLog.Append = true
## \u53EA\u8F93\u51FADEBUG\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7
log4j.appender.UserLog.Threshold = DEBUG
log4j.appender.zhangsanLog.MaxFileSize = 10KB
log4j.appender.zhangsanLog.MaxBackupIndex = 5
#'.'yyyy-MM-dd: \u6BCF\u5929\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6
log4j.appender.UserLog.DatePattern = '.'yyyy-MM-dd
log4j.appender.UserLog.layout = org.apache.log4j.PatternLayout
log4j.appender.UserLog.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%t:%r] - [%p] [%c{1}:%L] [%M] %m%n
log4j.additivity.UserLog = false
具体请参考点击打开链接
3、java文件中使用:
public static Logger logger=Logger.getLogger(MyArrayList.class);
logger.info("删除了"+count+"个指定元素"); //另外还有.trace等等,还得以后用到