Hi

I think that it can be done by having your own Appender class 
something like

import org.apache.log4j.RollingFileAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;

import java.io.IOException;
import java.io.Writer;
import java.io.File;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;

import org.apache.log4j.helpers.OptionConverter;
import org.apache.log4j.helpers.QuietWriter;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.helpers.CountingQuietWriter;
import org.apache.log4j.spi.LoggingEvent;

public class DateTimeFileAppender extends FileAppender {

  static final public String LOG_DATE_FORMAT = "LogDateFormat";
  static final public String LOG_FILE_FREQUENCY = "LogFileFrequency";

  protected String logDateFormat = "yyyyMMdd_HHmmss" ;
  protected long logFileFreq = 2*60*60 ;
  public void setLogDateFormat(String logDateFormatStr){
        logDateFormat = logDateFormatStr;
   }
  public void setLogFileFrequency(String logFileFrequencyStr){
        logFileFrequency = logFileFrequencyStr;
   }


   public void makeBackFileWithTimeStamp() {
        File target, file;

        SimpleDateFormat theFormatter = new SimpleDateFormat(logDateFormat);
        Date theTime = Calendar.getInstance().getTime();

        // Backup File name,
        int hasDot = fileName.lastIndexOf(".") ;
        int len = hasDot > 0 ? hasDot : fileName.length();
        String newName = fileName.substring( 0, len ) + "_" +
                      theFormatter.format( theTime ) + ".log";

        LogLog.debug("making back up of log with name:"+newName);
        // make sure to close handle before renaming
        this.closeFile();
        File backLogFile = new File(newName);
        File curLogFile = new File(fileName);
        curLogFile.renameTo(backLogFile);

        try {
          // This will also close the file. This is OK since multiple
          // close operations are safe.
          this.setFile(fileName, false);
        }
        catch(IOException e) {
          LogLog.error("setFile("+fileName+", false) call failed.", e);
        }
    }
..
...
other methods
...
    protected void subAppend(LoggingEvent event) {
        super.subAppend(event);

        if((fileName != null) &&
                                // check time elapsed
                         timeElapsed >= logFileFrequency)
          this.makeBackFileWithTimeStamp();
    }
    
}

I hope that it works for you
Please tell me if I am wrong
-Kapil

-----Original Message-----
From: Ceki Gülcü [mailto:[EMAIL PROTECTED]]
Sent: 05 January 2002 15:37
To: Log4J Users List
Subject: Re: Logging to multiple files?



I am afraid this requirement never came up previously. Would you care to
expand on the requirement?

Regards, Ceki

At 10:36 05.01.2002 +0100, you wrote:
>Hi,
>I need to log to files based on current date, but in case where program
>restarts it must create a new file based on current time, for example:
>
>1st start:
>2002-01-01_00-00.log
>2002-01-02_00-00.log
>
>stop, 2nd start:
>2002-01-02_16-43.log
>2002-01-03_00-00.log
>2002-01-04_00-00.log
>
>Is it possible with out-of-box Log4j?
>
>Tkanks in advance, greetings,
>     deacon Marcus
>
>
>--
>To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
>For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>

--
Ceki Gülcü - http://qos.ch



--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

This message may contain privileged and/or confidential information.  If you
have received this e-mail in error or are not the intended recipient, you
may not use, copy, disseminate, or distribute it; do not open any
attachments, delete it immediately from your system and notify the sender by
e-mail promptly that you have done so.  Thank You.

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to