我想在每天的日志文件末尾添加时间戳。 日志文件应该是每天的意思 test.2013-01-10.log
我的新log.properties文件如下。请帮助
log4j.rootLogger=info,myapp
log4j.appender.myapp=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myapp.ImmediateFlush=true
log4j.appender.myapp.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.myapp.layout.ConversionPattern=%d{yyyy-MM-dd}%m%n
log4j.appender.myapp.file=${catalina.base}/logs/myapplog/test
log4j.appender.myapp.DatePattern='_'yyyy-MM-dd
#log4j.appender.myapp.MaxFileSize=999MB
#log4j.appender.myapp.MaxBackupIndex=20
log4j.appender.myapp.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%m%n
log4j.logger=info,stdout,myapp
log4j.logger.org.hibernate=warn
答案 0 :(得分:0)
在配置中尝试使用DailyRollingFileAppender而不是RollingFileAppender
编辑:
试试这个。它将生成test.log,当您的计算机日期发生变化时,它将生成新的test.log和test.logyyyy-mm-dd
log4j.properties:
log4j.rootLogger=info,A1
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.ImmediateFlush=true
log4j.appender.A1.DatePattern='.'%d{yyyy-mm-dd}
log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd}%m%n
log4j.appender.A1.File=c:/test.log
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
#log4j.appender.A1.MaxFileSize=999MB
#log4j.appender.A1.MaxBackupIndex=20
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%m%n
log4j.logger=info,stdout,A1
log4j.logger.org.hibernate=warn
代码:
import org.apache.log4j.Logger;
public class test {
public static void main(String[] args) {
Logger barlogger = Logger.getLogger(test.class);
barlogger.info("test");
}
}
<强>更新强>
如果要将日志更改为.txt,请更改:
log4j.appender.A1.File=${catalina.base}/logs/test.log
为:
log4j.appender.A1.File=${catalina.base}/logs/test.txt