user1800059
user1800059

Reputation: 43

Custom log4j appender

i wrote my personal log4j Daily Rolling Appender in order to customize the logging for my app.

Here is the source code for the 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;
}

here is the log4j.properties

FILE is set to be 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

where MYAPPENDER is my custom appender.

Now the problem is that sometimes i notice that the output logfile are truncated despite the FILE.Append is true

Thanks in advance

Regards

Upvotes: 0

Views: 976

Answers (1)

Display Name is missing
Display Name is missing

Reputation: 6227

This is happening because you are using DailyRollingFileAppender. You need to set your log4j.appender.File.DatePattern

"For example, if the File option is set to /foo/bar.log and the DatePattern set to '.'yyyy-MM-dd, on 2001-02-16 at midnight, the logging file /foo/bar.log will be copied to /foo/bar.log.2001-02-16 and logging for 2001-02-17 will continue in /foo/bar.log until it rolls over the next day.

Is is possible to specify monthly, weekly, half-daily, daily, hourly, or minutely rollover schedules."

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

Upvotes: 1

Related Questions