且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

需要一个通过属性配置log4j RollingFileAppender的工作示例

更新时间:2022-05-22 00:31:45

我相信我只是配置错误;没有人有配置RollingFileAppender的有效示例吗?

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

这对我@mcherm似乎很好.见下文.

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

您是否确定正在使用自己认为的log4j.properties?尝试将.File更改为另一个路径,以查看日志输出是否进入新文件.您使用的是哪个版本的log4j?我正在运行1.2.15.

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.

希望这会有所帮助.

我创建了以下测试程序:

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

我的log4j.properties文件保存:

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

请注意,我删除了对RollingFileAppender无效的DatePattern.我的布局是:

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() {
    }
}

使用-Dcatalina.base=/tmp/运行时,在/tmp/logs/中得到的文件上升到索引#5,大小为10mb.如果我调MaxFileSizeMaxBackupIndex,它会适当调整.

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.