log4j,每个日志条目的时间戳

时间:2008-12-11 01:44:40

标签: java python log4j jython

这是我的日志输出

INFO main digestemails - process inbox
INFO main digestemails - checking for emails in c:\development\DCMail\email\KN-Source
INFO main digestemails - digesting 003d01c95a7b_3446880_0202fea9@xxxx.com.eml
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 

我该怎么做?

3 个答案:

答案 0 :(得分:96)

在PatternLayout中使用%d。

同样%d可以采用格式模式,如%d {dd MMM yyyy HH:mm:ss,SSS},您可以选择所需的元素。省略格式模式时,日期将采用ISO8601格式。

答案 1 :(得分:16)

我的属性文件

的摘录
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

答案 2 :(得分:6)

您可以在log4j javadoc中找到更多转换字符用法。例如,在http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

豫ICP备18024241号-1