Java 在 log4j 中使用 FileNamePattern、RollingFileAppender

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4324427/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:53:58  来源:igfitidea点击:

Using FileNamePattern, RollingFileAppender in log4j

javalogginglog4j

提问by jasonline

I have a log file named 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.logbut it's not working. Below is my log4j settings:

我有一个名为 app.log 的日志文件。当它滚动时(我将它设置为每分钟只是为了测试目的),我希望它被重命名,app-YYYY-MM-dd_HH-mm.log但它不起作用。以下是我的 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.

回答by Martin Algesten

Try removing logs/from both .Fileand .FileNamePattern. I'm reading the code, and it looks like it should work, but it may be worth reducing the problem.

尝试logs/.File和 中删除.FileNamePattern。我正在阅读代码,看起来它应该可以工作,但可能值得减少这个问题。

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

回答by darioo

I assume you're using justlog4j. Version 1.2.16 is the newest one. rollingPolicydoesn't exist in its source code; only in log4j.dtdfile for xml based configuration.

我假设您使用log4j。版本 1.2.16 是最新版本。rollingPolicy不存在于其源代码中;仅在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.

这条路径的缺点是您的日志文件不会被压缩。

回答by Paul

According to the log4j wiki:

根据log4j wiki

Note that TimeBasedRollingPolicy can only be configured with xml, not log4j.properties

注意TimeBasedRollingPolicy只能配置xml,不能配置log4j.properties

The APIdoesn't mention that, but perhaps that's the problem?

API没有提到这一点,但也许这就是问题所在?

回答by karu

Download log4jextras jar file and put it into lib folder. Also add the rollingPolicytag a follows:

下载log4jextras jar 文件并将其放入 lib 文件夹。还要添加rollingPolicy标签 a 如下:

<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>

回答by Ahmad Nadeem

Please check you have included apache-log4j-extras.jarand using log4j-1.2.16.jaror at least above 2.17 version. here is sample log4j.properties which can be used.

请检查您是否包含apache-log4j-extras.jar并使用log4j-1.2.16.jar或至少高于 2.17 版本。这是可以使用的示例 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

有关属性的更多详细信息,请查看此处

回答by user9880478

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..");      
 }