简体   繁体   English

在log4j中使用FileNamePattern,RollingFileAppender

[英]Using FileNamePattern, RollingFileAppender in log4j

I have a log file named app.log. 我有一个名为app.log的日志文件。 When it rolls over (I'm setting it to every minute just for testing purposes), I would like it to be renamed to app-YYYY-MM-dd_HH-mm.log but it's not working. 当它翻身时(我将其设置为每分钟仅用于测试目的),我希望将其重命名为app-YYYY-MM-dd_HH-mm.log但它不起作用。 Below is my log4j settings: 以下是我的log4j设置:

log4j.appender.myLog=org.apache.log4j.RollingFileAppender
log4j.appender.myLog.rollingPolicy=TimeBasedRollingPolicy
log4j.appender.myLog.File=logs/app.log
log4j.appender.myLog.rollingPolicy.FileNamePattern=logs/app-%d{yyyy-MM-dd_HH-mm}.log
log4j.appender.myLog.Append=true
log4j.appender.myLog.layout=org.apache.log4j.PatternLayout
log4j.appender.myLog.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n

Does anyone know what's the problem? 有谁知道这是什么问题? During the rollover, it just renames the file into app.log.1 . 在翻转期间,它只是将文件重命名为app.log.1

I assume you're using just log4j. 我假设你使用log4j。 Version 1.2.16 is the newest one. 版本1.2.16是最新版本。 rollingPolicy doesn't exist in its source code; rollingPolicy在其源代码中不存在; only in log4j.dtd file for xml based configuration. 仅在log4j.dtd文件中进行基于xml的配置。

The only way you'll get what you want to work is to download Apache extras companion for log4j . 你能得到你想要的工作的唯一方法是下载log4j的Apache extras伴侣


Eventually if you don't want to use extras You can workaround using: 最终如果您不想使用附加功能您可以使用以下方法解决:

org.apache.log4j.DailyRollingFileAppender org.apache.log4j.DailyRollingFileAppender

Minus of this path is that your log files won't be gzipped. 减少此路径是您的日志文件不会被gzip压缩。

According to the log4j wiki : 根据log4j维基

Note that TimeBasedRollingPolicy can only be configured with xml, not log4j.properties 请注意,TimeBasedRollingPolicy只能使用xml配置,而不能使用log4j.properties配置

The API doesn't mention that, but perhaps that's the problem? API没有提到这一点,但也许这就是问题所在?

Download log4j extras jar file and put it into lib folder. 下载log4j extras jar文件并将其放入lib文件夹。 Also add the rollingPolicy tag a follows: 另外添加如下的rollingPolicy标记:

<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
    <param name="FileNamePattern" 
       value="D:/Apps/Enterprise_domain/diagnostics/logs/diagnostics.% d{yyyy-MM-dd_HH-mm}.log"/>
</rollingPolicy>

Please check you have included apache-log4j-extras.jar and using log4j-1.2.16.jar or at least above 2.17 version. 请检查您是否已包含apache-log4j-extras.jar并使用log4j-1.2.16.jar或至少高于2.17版本。 here is sample log4j.properties which can be used. 这里是可以使用的示例log4j.properties。

    #Worked with 2.17 version
    #make log files rotate every minute or hour and zip old rotated logs
    log4j.rootLogger=INFO, loggerId
    log4j.appender.loggerId=org.apache.log4j.rolling.RollingFileAppender
    log4j.appender.loggerId.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
    log4j.appender.loggerId.rollingPolicy.ActiveFileName=worker.log
    log4j.appender.loggerId.rollingPolicy.FileNamePattern=worker-.%d{yyyyMMdd-HHmm}.log.gz
    log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout
    log4j.appender.loggerId.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n

for more details on properties please check here 有关房产的更多详情,请点击此处

Try removing logs/ from both .File and .FileNamePattern . 尝试从.File.FileNamePattern删除logs/ I'm reading the code, and it looks like it should work, but it may be worth reducing the problem. 我正在阅读代码,它看起来应该可行,但可能值得减少问题。

https://svn.apache.org/repos/asf/logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rolling/TimeBasedRollingPolicy.java https://svn.apache.org/repos/asf/logging/log4j/companions/extras/trunk/src/main/java/org/apache/log4j/rolling/TimeBasedRollingPolicy.java

System.out.println("Loggers initiallization process started..");
 if(objApploger == null){
     objApploger = new AppLogger();
     String loglevel="ERROR";
     String logPath="E:\\Examples\\applicationLogs";
     String logMaxSize="50000";//in kbs
     int nmaxbackupIndex=20;
     String conversionPattern="%d{yyyy-MM-dd HH:mm:ss:SSS} %-5p  :: %m%n";       
     RollingFileAppender RFAppender= null;
     RFLog =Logger.getLogger("Log");
     RFLog.setLevel(Level.toLevel(loglevel));       
     Calendar cal= Calendar.getInstance();       
     String timeFrame=cal.get(5)+"_"+(cal.get(2)+1)+"_"+cal.get(1);
     logPath=logPath+"TestLog_"+timeFrame+".log";
     RFAppender = new RollingFileAppender(new PatternLayout(conversionPattern),logPath);
     RFAppender.setMaxBackupIndex(nmaxbackupIndex);
     RFAppender.setMaxFileSize(logMaxSize);
     RFLog.addAppender(RFAppender);
     System.out.println("Loggers initiallization process completed..");      
 } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM