1

我想每天在我的日志文件末尾添加时间戳。意味着每天的日志文件应该看起来像 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
4

1 回答 1

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
于 2013-01-10T05:10:34.437 回答