0

My company uses a software package that reads in log files from our servers, parses them, and spits performance data into a database. We dont have access / permission to modify the source code for the app that reads the files but we do have access to the code that writes the files. I need to change the way the log files are being written and I would like to use log4j (so I can use an AsyncAppender). The program expects a few things:

1). There should be 10 log files that roll and each log file will be one day of logs. The files need to be named 0 through 9 and I need to be able to programatically set the file name and when they roll based on the server time.

2). Essentially when generating the 11th log file it should delete the oldest one and start writing to that one.

3). When a new log file is generated I need to be able to insert a timestamp as the first line of the file (System.currentTimeMillis()).

Is it possible to meet the above requirements with a custom log4j file appender? Ive looked at DailyRollingFileAppender but cant seem to figure out how to control the file names exactly like I need to. Also I cant seem to figure out how to write the first line in the log when it is generated (for example is there some callback function I can register when a new log file gets rolled in)?

George
  • 1,021
  • 15
  • 32
  • check here : http://stackoverflow.com/questions/8798794/log-file-name-to-include-current-date-in-log4j – Pat B Nov 08 '13 at 20:39

2 Answers2

0

I think you can achieve first 2 with

using RollingFileAppender and specifying FixedWindowRollingPolicy for RollingPolicy

as for the #3 you can always write your own handler

Алексей
  • 1,847
  • 12
  • 15
0

For the sake of posterity. I used the below class as my custom rolling policy

import org.apache.log4j.rolling.RollingPolicyBase;
import org.apache.log4j.rolling.RolloverDescription;
import org.apache.log4j.rolling.RolloverDescriptionImpl;
import org.apache.log4j.rolling.TriggeringPolicy;
import org.apache.log4j.Appender;
import org.apache.log4j.spi.LoggingEvent;

public final class CustomRollingPolicy extends RollingPolicyBase
implements TriggeringPolicy
{


private short curFileId = -1;
private String lastFileName = null;

static private final long FILETIMEINTERVAL = 86400000l;
static private final int NUM_FILES = 10;//86400000l;
public String folderName = "";

public String getFolderName() {
return folderName;
}

public void setFolderName(String folderName) {
    this.folderName = folderName;
}

private short calculateID(long startTime) {
    return (short) ((startTime / FILETIMEINTERVAL) % NUM_FILES);
}


 public String getCurrentFileName()
  {
      StringBuffer buf = new StringBuffer();
  buf.append(folderName);
  buf.append(calculateID(System.currentTimeMillis()));
  return buf.toString();
  }

  public void activateOptions()
  {
super.activateOptions();
this.lastFileName = getCurrentFileName();
  }

  public RolloverDescription initialize(String currentActiveFile, boolean append)
  {
    curFileId = this.calculateID(System.currentTimeMillis());
    lastFileName = getCurrentFileName();
    String fileToUse = activeFileName != null? activeFileName: currentActiveFile != null?currentActiveFile:lastFileName;
    return new RolloverDescriptionImpl(fileToUse, append, null, null);
  }

public RolloverDescription rollover(String currentActiveFile)
{
    curFileId = this.calculateID(System.currentTimeMillis());

    String newFileName = getCurrentFileName();
    if (newFileName.equals(this.lastFileName)) 
    {
        return null;
    }

    String lastBaseName = this.lastFileName;

    String nextActiveFile = newFileName;

    if (!currentActiveFile.equals(lastBaseName)) 
    {
        nextActiveFile = currentActiveFile;
    }

    this.lastFileName = newFileName;
    return new RolloverDescriptionImpl(nextActiveFile, false, null, null);
}

public boolean isTriggeringEvent(Appender appender, LoggingEvent event, String filename, long fileLength)
{
    short fileIdForCurrentServerTime = this.calculateID(System.currentTimeMillis());
    return curFileId != fileIdForCurrentServerTime;
}
}

And here is the appender config in my log4j xml file:

    <!-- ROLLING FILE APPENDER FOR RUM LOGS -->
    <appender name="rumRollingFileAppender" class="org.apache.log4j.rolling.RollingFileAppender">       
        <rollingPolicy class="com.ntrs.wpa.util.CustomRollingPolicy">
            <param name="folderName" value="C:/bea-portal-10.3.2/logs/"/>
        <param name="FileNamePattern" value="C:/bea-portal-10.3.2/logs/foo.%d{yyyy-MM}.gz"/>
    </rollingPolicy>        
    <layout class="com.ntrs.els.log4j.AppServerPatternLayout">
          <param name="ConversionPattern" value="%m%n" />
    </layout>
  </appender>    
George
  • 1,021
  • 15
  • 32