自定义log4j appender

时间:2014-02-18 18:08:03

标签: java logging log4j rollingfileappender mdc

我写了我的个人log4j Daily Rolling Appender,以便自定义我的应用程序的日志记录。

以下是Appender的源代码

private final static String DOT = ".";
private final static String HIPHEN = "-";
private static final String ORIG_LOG_FILE_NAME = "OrginalLogFileName";

public MigrationFilterDailyRollingFileAppender()
{
}

public MigrationFilterDailyRollingFileAppender(Layout layout, String fileName,
    String datePattern)
    throws IOException
{
    super(layout, fileName, datePattern);
}

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

@Override
public void append(LoggingEvent event)
{
    try
    {

            setFile(appendMigrationToFileName((String) MDC.get(ORIG_LOG_FILE_NAME)), this.getAppend(), this.getBufferedIO(),
            this.getBufferSize());


    }
    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 appendMigrationToFileName(String oldLogFileName)
{
    if (oldLogFileName != null)
    {
        Object obj = MDC.get("MIGRATION");
        if (obj != null)
        {
            String level = (String)obj;
            final File logFile = new File(oldLogFileName);
            String newFileName = "";
            final String fn = logFile.getName();
            final int dotIndex = fn.indexOf(DOT);
            if (dotIndex != -1)
            {
                // the file name has an extension. so, insert the level
                // between the file name and the extension
                newFileName = fn.substring(0, dotIndex) + HIPHEN + level + DOT
                    + fn.substring(dotIndex + 1);
            }
            else
            {
                // the file name has no extension. So, just append the level
                // at the end.
                newFileName = fn + HIPHEN + level;
            }
            return logFile.getParent() + File.separator + newFileName;
        }
        else return oldLogFileName;
    }
    return null;
}

这是log4j.properties

FILE设置为DailyRollingFileAppender

log4j.appender.FILE=MYAPPENDER
log4j.appender.FILE.Threshold=DEBUG
log4j.appender.FILE.File=${it.fastweb.activity.node.nodeHome}/log/logFile.log
log4j.appender.FILE.Append=true
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss.SSS} %-5p [%t] %-17c{1} - %m%n

MYAPPENDER是我的自定义附加程序。

现在问题是,有时我注意到输出日志文件被截断,尽管FILE.Append为真

提前致谢

此致

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您正在使用DailyRollingFileAppender。您需要设置log4j.appender.File.DatePattern

"例如,如果将File选项设置为/foo/bar.log并将DatePattern设置为'。' yyyy-MM-dd,则在2001-02-16 at午夜,日志文件/foo/bar.log将被复制到/foo/bar.log.2001-02-16,并且将在/foo/bar.log中继续记录2001-02-17,直到它翻过下一个一天。

可以指定每月,每周,每半日,每日,每小时或每分钟翻转时间表。"

http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html