问题
I have my application for which the below entry is done in Log4j properties file.Size based rolling is not working for this application. File does not rollover at 5 MB and the application keeps on logging and making it in GB. In one run approximately 6 MB of log is generated.The application runs multiple times a day.
Can someone help in pointing out where I am doing a mistake?
# SampleApp configuration
log4j.category.com.ibm.hmel.hwmmphdya=INFO, SampleApp
log4j.additivity.com.ibm.hmel.hwmmphdya=false
log4j.appender.SampleApp =org.apache.log4j.rolling.RollingFileAppender
log4j.appender.SampleApp.File=S:/FIH/LOG/SAMPLE_APP_01/SAMPLE_APP_01.log
log4j.appender.SampleApp.layout=org.apache.log4j.PatternLayout
log4j.appender.SampleApp.layout.ConversionPattern=%-21d{dd-MMM-yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n
log4j.appender.SampleApp.RollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.SampleApp.RollingPolicy.FileNamePattern=S:/HIF/LOG/SAMPLE_APP_01 /SAMPLE_APP_01.log.%i.zip
log4j.appender.SampleApp.RollingPolicy.MinIndex=1
log4j.appender.SampleApp.RollingPolicy.MaxIndex=10
log4j.appender.SampleApp.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.SampleApp.TriggeringPolicy.MaxFileSize=5242880
回答1:
Following works for me
log4j.rootLogger=TRACE, Roller
log4j.appender.Roller=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.Roller.RollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.Roller.RollingPolicy.maxIndex=13
log4j.appender.Roller.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.Roller.TriggeringPolicy.MaxFileSize=26214400
log4j.appender.Roller.RollingPolicy.FileNamePattern=j_log_%i.log
log4j.appender.Roller.RollingPolicy.ActiveFileName=j_log_0.log
log4j.appender.Roller.layout=org.apache.log4j.PatternLayout
log4j.appender.Roller.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
回答2:
Try adding a unit to your size
MaxFileSize=5242880KB
回答3:
As i can see you're using lo4j companion extras. Please ensure you're using latest log4j.jar (version 1.2.17 worked for me and I've encountered problems with version 1.2.14).
I wrote simple test app to reproduce your problem:
package com.ibm.hmel.hwmmphdya;
import org.apache.log4j.Logger;
public class Roller {
private Logger logger = Logger.getLogger(Roller.class);
private final String FAKE_LOG_LINE = "12345678901234567890123456789012345678901234567890";
public static void main(String[] args) {
Roller roller = new Roller();
roller.logStuff();
}
private void logStuff() {
for (;;) { // ugly loop
logger.info(FAKE_LOG_LINE);
}
}
}
and placed following log4j.properties
in classpath:
log4j.category.com.ibm.hmel.hwmmphdya=INFO, Roller
log4j.additivity.com.ibm.hmel.hwmmphdya=false
log4j.appender.Roller=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.Roller.File=D:/FIH/SAMPLE_APP_01.log
log4j.appender.Roller.layout=org.apache.log4j.PatternLayout
log4j.appender.Roller.layout.ConversionPattern=%-21d{dd-MMM-yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n
log4j.appender.Roller.RollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.Roller.RollingPolicy.FileNamePattern=D:/FIH/SAMPLE_APP_01.log.%i.zip
log4j.appender.Roller.RollingPolicy.MinIndex=1
log4j.appender.Roller.RollingPolicy.MaxIndex=10
log4j.appender.Roller.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.Roller.TriggeringPolicy.MaxFileSize=5242880
My results are OK i think, theese are contents of my log output directory:
2013-02-27 09:30 <DIR> .
2013-02-27 09:30 <DIR> ..
2013-02-27 09:30 4 440 720 SAMPLE_APP_01.log
2013-02-27 09:30 5 242 920 SAMPLE_APP_01.log.1.zip
2013-02-27 09:30 5 242 920 SAMPLE_APP_01.log.2.zip
2013-02-27 09:30 5 242 920 SAMPLE_APP_01.log.3.zip
2013-02-27 09:30 5 242 920 SAMPLE_APP_01.log.4.zip
2013-02-27 09:30 5 242 920 SAMPLE_APP_01.log.5.zip
File size "5 242 920" is more or less what was configured.
You can troubleshoot your case further by providing -Dlog4j.debug
system property.
UPDATE:
You may want to ensure companion extras lib (apache-log4j-extras-1.1.jar
) is present in classpath.
回答4:
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 based on size and zip old rotated
log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.loggerId.rollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.loggerId.rollingPolicy.maxIndex=5
log4j.appender.loggerId.triggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.loggerId.triggeringPolicy.MaxFileSize=10000000
log4j.appender.loggerId.rollingPolicy.FileNamePattern=worker-%i.log.gz
log4j.appender.loggerId.rollingPolicy.ActiveFileName=worker.log
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
来源:https://stackoverflow.com/questions/15105694/log4j-configurationsize-based-not-working