自定义log4j滚动appender

时间:2012-02-03 05:50:43

标签: java logging log4j

我的班级代码如下。

public class LogLevelFilterFileAppender extends FileAppender implements Cloneable{

    private final static String DOT = ".";
    private final static String DASH = "-";
    private static final String ORIGINAL_FILE_NAME = "OrginalLogFileName";

    public LogLevelFilterFileAppender() {}

    public LogLevelFilterFileAppender(Layout layout, String fileName,
                                      boolean append, boolean bufferedIO, int bufferSize)
    throws IOException {
        new FileAppender(layout, fileName, append, bufferedIO, 10);
        new RollingFileAppender(layout, fileName,append);
    }

    public LogLevelFilterFileAppender(Layout layout, String fileName,
                                      boolean append) throws IOException {
        super(layout, fileName, append);
        new RollingFileAppender(layout, fileName,append);
    }

    public LogLevelFilterFileAppender(Layout layout, String fileName)
    throws IOException {
        super(layout, fileName);
        new RollingFileAppender(layout, fileName);
    }

    @Override
    public void activateOptions() {
        MDC.put(ORIGINAL_FILE_NAME, fileName);
        super.activateOptions();
    }

    @Override
    public void append(LoggingEvent event) {
        try {
            setFile(appendLevelToFileName((String) MDC.get(ORIGINAL_FILE_NAME),
                    event.getLevel().toString()), fileAppend, bufferedIO,
                    bufferSize);
        }
        catch (IOException ie) {
            errorHandler
            .error(
                    "Error occured while setting file for the log level "
                    + event.getLevel(), ie,
                    ErrorCode.FILE_OPEN_FAILURE);
        }
        super.append(event);
    }

    private String appendLevelToFileName(String oldLogFileName, String level) {
        if (oldLogFileName != null) {
            final File logFile = new File(oldLogFileName);
            String newFileName = "";
            final String fn = logFile.getName();
            final int dotIndex = fn.indexOf(DOT);
            if (dotIndex != -1) {
                newFileName = fn.substring(0, dotIndex) + DASH + level + DOT
                + fn.substring(dotIndex + 1);
            }
            else {
                newFileName = fn + DASH + level;
            }
            return logFile.getParent() + File.separator + newFileName;
        }
        return null;
    }
}

我的log4j.properties配置如下。

log4j.rootLogger = DEBUG, fileout
log4j.appender.fileout = com.elitecore.mediation.util.log.LogLevelFilterFileAppender
log4j.appender.fileout.layout.ConversionPattern = %d{MMMMM dd,yyyy HH:mm:ss} %-5p [ %C{1} - %M() - %L ] - %m%n
# log4j.appender.fileout.layout.ConversionPattern = %d{MMMMM dd,yyyy HH:mm:ss} %-5p %c %M():%L - %m%n
# %r [%t] %-5p %c %x - %m%n
# %d{MMMMM dd HH:mm:ss} %-5p %c %M():%L - %m%n
#%d{MMMMM dd HH:mm:ss} %6p %c %M() : %m%n
log4j.appender.fileout.layout = org.apache.log4j.PatternLayout
log4j.appender.fileout.File = /usr/local/mediation/logs/mediation-logs.log

这是为不同类型创建日志,例如,DEBUG,ERROR和INFO在不同的日志文件中。但它有什么限制?它正在创建越来越大的日志文件。我想制作5 MB的日志文件,并删除以前的日志。我怎样才能做到这一点?当我尝试使用RollingFile Appender时,我只获得以下日志文​​件。

[root@manage logs]# ls -1
mediation-logs-DEBUG.log
mediation-logs-ERROR.log
mediation-logs-INFO.log
mediation-logs-INFO.log.1
mediation-logs-INFO.log.2
mediation-logs.log

滚动日志文件ERRORDEBUG未完成,但INFO已完成。

1 个答案:

答案 0 :(得分:1)

我建议您从RollingFileAppender而不是FileAppender派生。这将使您可以定义日志文件的增长量以及要保留的“旧”文件数量。请稍后查看the manual如何在log4j.propertiers中使用它。

如果我理解正确,你需要每个日志级别一个文件,这是正确的吗?如果是这样,我建议你按照这个FAQ-Entry而不是“滚动”你自己的解决方案:)

Matching exact log-levels