1、log4j包导入
百度网盘:https://pan.baidu.com/s/1OEqbE1-2jzECbzBPpY1_lA
密码:f4w0
2、Src同级创建并设置log4j.properties
1 ### 设置### 2 log4j.rootLogger = debug,stdout,D,E 3 4 ### 输出信息到控制台 ### 5 log4j.appender.stdout = org.apache.log4j.ConsoleAppender 6 log4j.appender.stdout.Target = System.out 7 log4j.appender.stdout.layout = org.apache.log4j.PatternLayout 8 log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n 9 10 ### 输出DEBUG 级别以上的日志到=D://logs/error.log ### 11 log4j.appender.D = org.apache.log4j.DailyRollingFileAppender 12 log4j.appender.D.File = D://logs/log.log 13 log4j.appender.D.Append = true 14 log4j.appender.D.Threshold = DEBUG 15 log4j.appender.D.layout = org.apache.log4j.PatternLayout 16 log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n 17 18 ### 输出ERROR 级别以上的日志到=D://logs/error.log ### 19 log4j.appender.E = org.apache.log4j.DailyRollingFileAppender 20 log4j.appender.E.File =D://logs/error.log 21 log4j.appender.E.Append = true 22 log4j.appender.E.Threshold = ERROR 23 log4j.appender.E.layout = org.apache.log4j.PatternLayout 24 log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
3、日志内容设置
1 private static Logger logger = Logger.getLogger(MyPerson.class); 2 /** 3 * @param args 4 */ 5 public static void main(String[] args) { 6 // System.out.println("This is println message."); 7 8 // 记录debug级别的信息 9 logger.debug("This is debug message."); 10 // 记录info级别的信息 11 logger.info("This is info message."); 12 // 记录error级别的信息 13 logger.error("This is error message."); 14 }
4、输出结果和文件查看