Log4j configuration(Size Based) not working

社会主义新天地 提交于 2019-12-05 17:55:52

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

Try adding a unit to your size

MaxFileSize=5242880KB

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!