Using FileNamePattern, RollingFileAppender in log4j
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.log
but it's not working. Below is my log4j settings:
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
log4开发者_如何学JAVAj.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
.
According to the log4j wiki:
Note that TimeBasedRollingPolicy can only be configured with xml, not log4j.properties
The API doesn't mention that, but perhaps that's the problem?
Download log4j
extras jar file and put it into lib folder.
Also add the rollingPolicy
tag a follows:
<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>
I assume you're using just log4j. Version 1.2.16 is the newest one. rollingPolicy
doesn't exist in its source code; only in log4j.dtd
file for xml based configuration.
The only way you'll get what you want to work is to download Apache extras companion for log4j.
Eventually if you don't want to use extras You can workaround using:
org.apache.log4j.DailyRollingFileAppender
Minus of this path is that your log files won't be gzipped.
Please check you have included apache-log4j-extras.jar and using log4j-1.2.16.jar or at least above 2.17 version. here is sample log4j.properties which can be used.
#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
. 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
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..");
}
精彩评论