Java log4j 带有每个日志条目的时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/358225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4j with timestamp per log entry
提问by Setori
this is my log output
这是我的日志输出
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 -
I would like a time stamp per log message ie
我想要每个日志消息的时间戳,即
INFO 2008-12-25 13:14:00 digestemails - email -> COMPLETED folder
here is my log4j config file
这是我的 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
How do I do it?
我该怎么做?
采纳答案by joshperry
Use %d
in your PatternLayout.
%d
在您的 PatternLayout 中使用。
Also %d
can take a format pattern as in %d{dd MMM yyyy HH:mm:ss,SSS}
you can pick and choose the elements that you want. When the format pattern is omitted the date will be in ISO8601 format.
也%d
可以采用格式模式,因为%d{dd MMM yyyy HH:mm:ss,SSS}
您可以选择所需的元素。当省略格式模式时,日期将采用 ISO8601 格式。
回答by rguo
You can find more conversion characters usage in log4j javadoc.For example, at http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html.
您可以在 log4j javadoc 中找到更多转换字符的用法。例如,在http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html。
回答by Markus Lausberg
A extract from my properties file
从我的属性文件中摘录
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