2008-12-11 69 views
45

這是我的日誌輸出log4j的每個日誌條目的時間戳

INFO main digestemails - process inbox 
INFO main digestemails - checking for emails in c:\development\DCMail\email\KN-Source 
INFO main digestemails - digesting [email protected] 
INFO main digestemails - extracting attachments 
INFO main digestemails - no attachments or no attachments supported 
INFO main digestemails - updating database 
INFO main digestemails - email -> COMPLETED folder 
INFO main digestemails - 

我想每個日誌消息的時間戳即

INFO 2008-12-25 13:14:00 digestemails - email -> COMPLETED folder 

這裏是我的log4j的配置文件

log4j.rootLogger=debug, stdout, R 

log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 

# Pattern to output the caller's file name and line number. 
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n 

log4j.appender.R=org.apache.log4j.RollingFileAppender 
log4j.appender.R.File=c:\\development\\DCMail\\logs\\digestlogfolder\\digest-logfile.log 

log4j.appender.R.MaxFileSize=100KB 
# Keep one backup file 
log4j.appender.R.MaxBackupIndex=1 

log4j.appender.R.layout=org.apache.log4j.PatternLayout 
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 

我該怎麼做?

回答

83

在PatternLayout中使用%d。

另外,%d可以採用格式模式,如%d {dd MMM yyyy HH:mm:ss,SSS},您可以選擇並選擇所需的元素。當格式模式被省略時,日期將採用ISO8601格式。

+1

只是爲了增加這方面的一個提取物,我想使我的課的簽名小,所以我用'%d {DD MMM YYYY HH:MM:SS,SSS}% -4r [%t]%-5p%c {1}%x - %m%n`。 ** {1} **僅打印c,而不打印類簽名的a.b.c。 – Siddharth 2013-05-04 06:37:53

15

從我的屬性文件

log4j.rootLogger=INFO, stdout, logfile 

log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d %p (%t) [%c] - %m%n 

log4j.appender.logfile=org.apache.log4j.RollingFileAppender 
log4j.appender.logfile.File=C:/log/client.log 
log4j.appender.logfile.MaxFileSize=5MB 
log4j.appender.logfile.MaxBackupIndex=0 
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout 
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n