1 导入Log4j ,我这版本是1.2.17 自己选择版本 **别告诉我不会导入
2 Path 奶瓶
3 创建一个与src同目录文件 命名为 log4.properties
4 文件的内容是, 直接复制,
1 ### \u8BBE\u7F6E### 2 log4j.rootLogger = debug,stdout,D,E 3 4 ### \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC ### 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 ### \u8F93\u51FADEBUG \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ### 11 log4j.appender.D = org.apache.log4j.DailyRollingFileAppender 12 log4j.appender.D.File = E://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 ### \u8F93\u51FAERROR \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ### 19 log4j.appender.E = org.apache.log4j.DailyRollingFileAppender 20 log4j.appender.E.File =E://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
**** E://logs/log.log 这是保存的log日子路径,使用之前创建logs文件夹5 使用方法
1 package com.net.www; 2 import org.apache.log4j.Logger; 3 4 public class testday { 5 private static Logger logger = Logger.getLogger(testday.class); 6 7 /** 8 * @param args 9 */ 10 public static void main(String[] args) { 11 // System.out.println("This is println message."); 12 13 // 记录debug级别的信息 14 logger.debug("This is debug message."); 15 // 记录info级别的信息 16 logger.info("This is info message."); 17 // 记录error级别的信息 18 logger.error("This is error message."); 19 } 20 }
6 运行结果如下
时间: 2024-10-06 01:10:07