Need a working example of configuring log4j Rollin

2019-03-11 11:05发布

问题:

I am using log4j for logging, and a property file for configuration. Currently, my log files are too big (3.5 GB is too large for a log file). So think I need to use RollingFileAppender - but when I do so the log file continues to grow overly large. I believe I have just misconfigured it; does anyone have a working example of configuring RollingFileAppender?

For the record, my current configuration looks like this:

log4j.appender.MAIN_LOG.File=${catalina.base}/logs/webtop.log
log4j.appender.MAIN_LOG=org.apache.log4j.RollingFileAppender
log4j.appender.MAIN_LOG.layout=com.mycompany.util.log.Log4JSimpleLayout
log4j.appender.MAIN_LOG.DatePattern='.'yyyy-MM-dd
log4j.appender.MAIN_LOG.MaxFileSize=10MB
log4j.appender.MAIN_LOG.MaxBackupIndex=99
log4j.appender.MAIN_LOG.append=true
log4j.rootCategory=ALL, MAIN_LOG

An alternative to RollingFileAppender would also be a fine solution.

回答1:

I believe I have just misconfigured it; does anyone have a working example of configuring RollingFileAppender?

This seems to work fine for me @mcherm. See below.

Are you sure that you are using the log4j.properties that you think you are? Try changing the .File to another path to see if log output goes to the new file. What version of log4j are you using? I'm running 1.2.15.

Hope this helps.


I created the following test program:

package com.j256.ormlite;
import org.apache.log4j.Logger;
public class Foo {
    private static Logger logger = Logger.getLogger(Foo.class);
    public static void main(String[] args) {
        for (int x = 0; x < 10000000; x++) {
            logger.error("goodness this shouldn't be happening to us right here!!!!");
        }
    }
}

My log4j.properties file holds:

log4j.appender.MAIN_LOG=org.apache.log4j.RollingFileAppender
log4j.appender.MAIN_LOG.File=${catalina.base}/logs/webtop.log
log4j.appender.MAIN_LOG.layout=com.j256.ormlite.Log4JSimpleLayout
log4j.appender.MAIN_LOG.MaxFileSize=10MB
log4j.appender.MAIN_LOG.MaxBackupIndex=5
log4j.appender.MAIN_LOG.append=true
log4j.rootCategory=ALL, MAIN_LOG

Notice that I removed the DatePattern which wasn't valid for my RollingFileAppender. My layout is:

package com.j256.ormlite;
import org.apache.log4j.spi.LoggingEvent;
public class Log4JSimpleLayout extends org.apache.log4j.Layout {
    @Override
    public String format(LoggingEvent event) {
        return "log message = " + event.getMessage().toString() + "\n";
    }
    @Override
    public boolean ignoresThrowable() {
        return true;
    }
    public void activateOptions() {
    }
}

Running with -Dcatalina.base=/tmp/ I get files in /tmp/logs/ which go up to index #5 and are 10mb in size. If I tune the MaxFileSize or the MaxBackupIndex, it adjusts appropriately.



回答2:

Your issue might be with the fact that you are specifying a DatePattern. The DatePattern is meant to be used with the DailyRollingFileAppender to specify the date that the log file should roll. I don't believe it can be used in conjunction with the MaxFileSize and MaxBackupIndex attributes. Log4j lets you roll files based on file size or date but not both.



回答3:

When we need log files to be rolled on a daily basis, we should be using DailyRollingFileAppender instead of RollingFileAppender. You do not need to specify the MaxFileSize limit instead only DatePattern is enough for rolling files based on frequency. I have tried the below configuration in log4j.properties file for rolling log files every minute.

log4j.appender.infoAppender=org.apache.log4j.DailyRollingFileAppender

log4j.appender.infoAppender.Threshold=INFO

log4j.appender.infoAppender.DatePattern='.' yyyy-MM-dd HH-mm

log4j.appender.infoAppender.File=C:/logs/info.log



回答4:

Start by setting the -Dlog4j.debug JVM parameter. That prints out a few useful lines of debug information showing which config file it's found and is using, etc. That should give you some clues to what's going wrong.

See http://logging.apache.org/log4j/1.2/manual.html