You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Oleg Zaidner <Ol...@iniru.com> on 2001/03/26 17:39:40 UTC

RollingFileAppender with zip abilities.

Hello everyone.

I am using log4j already in my second project.
At last time we found very big problem to manage logs on remote machine with
low-band connection.
So before open the log I have to commpress it and than download.
So I extend the  RillingFileAppender like shown later:

If I am right , or you going to make more flexible rollingfileappender that
will be more easy to extend?

Thank you for you.

With best wishes Oleg Zaidiner.
mailto:olegz@iniru.com
Software Engineer InirU Ltd.


public class RollingZipFileAppender extends RollingFileAppender
{

  /**
   * move current file to current.1.zip and rename all the files ".x.zip"
   * to ".x+1.zip"...
   */
  public // synchronization not necessary since doAppend is already synched
  void rollOver() {
    File target;    
    File file;

    // If maxBackups <= 0, then there is no file renaming to be done.
    if(maxBackupIndex > 0) {
      // Delete the oldest file, to keep Windows happy.
      file = new File(fileName + '.' + maxBackupIndex + ".zip"); // added by
Oleg
      if (file.exists())
       file.delete();

      // Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3,
2}
      for (int i = maxBackupIndex - 1; i >= 1; i--) {
    	file = new File(fileName + "." + i + ".zip" ); // added by Oleg
    	if (file.exists()) {
    	  target = new File(fileName + '.' + (i + 1) + ".zip" ); // added by
Oleg
    	  file.renameTo(target);
    	}
    }

      // Rename fileName to fileName.1
      target = new File(fileName + "." + 1);

      this.closeFile(); // keep windows happy. 

      file = new File(fileName);
      file.renameTo(target);

      zipFile(target.getPath()); // added by Oleg
    }
    
    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);
    }
  }


 public void zipFile(String filePath) {
		// Using try is required because of file io.
    FileOutputStream fos = null;
    ZipOutputStream zos = null;

		try {
      // Init Zip file stream.
      fos = new FileOutputStream(filePath+".zip");
		  zos = new ZipOutputStream(fos);
			// Create a file input stream and a buffered input
stream.
			FileInputStream fis = new FileInputStream(filePath);
			BufferedInputStream bis = new
BufferedInputStream(fis);

			// Create a Zip Entry and put it into the archive
(no data yet).
			ZipEntry fileEntry = new ZipEntry(filePath);
			zos.putNextEntry(fileEntry);

			// Create a byte array object named data and declare
byte count variable.
			byte[] data = new byte[1024];
			int byteCount;
			// Create a loop that reads from the buffered input
stream and writes
			// to the zip output stream until the bis has been
entirely read.
			while ((byteCount = bis.read(data, 0, 1024)) > -1) {
				zos.write(data, 0, byteCount);
			}

      // Close Zip output Stream
      zos.flush();
      zos.close();
      fos.close();

		} catch (IOException e) {
		}

		System.out.println(filePath);
	}

   
}