感恩前輩,下面就進行記錄:java
一、新建一個java工程,以下:
apache
import org.apache.log4j.Logger;app
import org.apache.log4j.PropertyConfigurator;ide
import org.apache.log4j.chainsaw.Main;spa
public class Test {debug
private static Logger logger = Logger.getLogger(Main.class); 日誌
public static void main(String[] args) {get
PropertyConfigurator.configure("log4j.properties"); /*這個我試一試了,如不要,感受日誌不能打印出來,若是添加這個就能輸出*/it
System.out.println("This is println message.");io
// 記錄debug級別的信息
logger.debug("This is debug message.......");
// 記錄info級別的信息
logger.info("This is info message......");
// 記錄error級別的信息
logger.error("This is error message.....");
}
}
log4j.properties內容以下:
l### 設置###
log4j.rootLogger = debug,stdout,D,E
### 輸出信息到控制擡 ###
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 = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### 輸出DEBUG 級別以上的日誌到=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logs//log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 輸出ERROR 級別以上的日誌到=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs//error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n