From d743c82c89e898788b65d097b5479143d331c56a Mon Sep 17 00:00:00 2001 From: hns Date: Wed, 22 Jun 2005 08:37:25 +0000 Subject: [PATCH] Go back to old log file rotation code layout a bit, but still only use one thread to gzip all rotated files: - GZipper class is with FileLogger again and takes either a list of files or a single file - Log file rotation is done synchronously in the Logging.Runner thread, only gzipping is done in a separate thread --- src/helma/util/FileLogger.java | 49 +++++++++++++++++- src/helma/util/Logging.java | 94 ++++++++-------------------------- 2 files changed, 68 insertions(+), 75 deletions(-) diff --git a/src/helma/util/FileLogger.java b/src/helma/util/FileLogger.java index ae5b62a1..600a7ea6 100644 --- a/src/helma/util/FileLogger.java +++ b/src/helma/util/FileLogger.java @@ -21,6 +21,7 @@ import org.apache.commons.logging.Log; import java.io.*; import java.text.*; import java.util.*; +import java.util.zip.GZIPOutputStream; /** * An extended Logger that writes to a file and rotates files each midnight. @@ -79,7 +80,7 @@ public class FileLogger extends Logger implements Log { File archive = rotateLogFile(); // gzip rotated log file in a separate thread if (archive != null) { - new Logging.FileGZipper(archive).start(); + new GZipper(archive).start(); } } // create a new log file, appending to an existing file @@ -252,4 +253,50 @@ public class FileLogger extends Logger implements Log { super.fatal(parm1, parm2); } + /** + * a Thread class that zips up a file, filename will stay the same. + */ + static class GZipper extends Thread { + List files; + final static int BUFFER_SIZE = 8192; + + public GZipper(List files) { + this.files = files; + setPriority(MIN_PRIORITY); + } + + public GZipper(File file) { + files = new ArrayList(1); + files.add(file); + setPriority(MIN_PRIORITY); + } + + public void run() { + Iterator it = files.iterator(); + File file = null; + + while (it.hasNext()) { + try { + file = (File) it.next(); + File zipped = new File(file.getAbsolutePath() + ".gz"); + GZIPOutputStream zip = new GZIPOutputStream(new FileOutputStream(zipped)); + BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); + byte[] b = new byte[BUFFER_SIZE]; + int len = 0; + + while ((len = in.read(b, 0, BUFFER_SIZE)) != -1) { + zip.write(b, 0, len); + } + + zip.close(); + in.close(); + file.delete(); + } catch (Exception e) { + System.err.println("Error gzipping " + file); + System.err.println(e.toString()); + } + } + } + } + } diff --git a/src/helma/util/Logging.java b/src/helma/util/Logging.java index 7554efb1..31a66c7b 100644 --- a/src/helma/util/Logging.java +++ b/src/helma/util/Logging.java @@ -19,7 +19,6 @@ package helma.util; import org.apache.commons.logging.*; import java.io.*; import java.util.*; -import java.util.zip.GZIPOutputStream; /** * Implementation of Jakarta Commons LogFactory that supports both @@ -173,30 +172,32 @@ public class Logging extends LogFactory { consoleLog = null; } - static void gzip(File file) { - final int BUFFER_SIZE = 8192; + /** + * Rotate log files on all registered logs + */ + static void rotateLogs() { + int nloggers = loggers.size(); + ArrayList files = new ArrayList(nloggers); - try { - File zipped = new File(file.getAbsolutePath() + ".gz"); - GZIPOutputStream zip = new GZIPOutputStream(new FileOutputStream(zipped)); - BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); - byte[] b = new byte[BUFFER_SIZE]; - int len = 0; + for (int i = nloggers - 1; i >= 0; i--) { + FileLogger log = (FileLogger) loggers.get(i); - while ((len = in.read(b, 0, BUFFER_SIZE)) != -1) { - zip.write(b, 0, len); + try { + File file = log.rotateLogFile(); + if (file != null) { + files.add(file); + } + } catch (IOException io) { + System.err.println("Error rotating log " + log.getName() + ": " + + io.toString()); } + } - zip.close(); - in.close(); - file.delete(); - } catch (Exception e) { - System.err.println("Error gzipping " + file); - System.err.println(e.toString()); + if (!files.isEmpty()) { + new FileLogger.GZipper(files).start(); } } - /** * Returns the timestamp for the next Midnight * @@ -215,8 +216,6 @@ public class Logging extends LogFactory { return cal.getTime().getTime(); } - - /** * The static runner class that loops through all loggers. */ @@ -230,7 +229,7 @@ public class Logging extends LogFactory { long now = System.currentTimeMillis(); if (nextMidnight < now) { - new LogRotator().start(); + rotateLogs(); nextMidnight = nextMidnight(); } @@ -264,58 +263,5 @@ public class Logging extends LogFactory { } } - - /** - * Log rotator thread calls rotateLogFiles on all - */ - static class LogRotator extends Thread { - - public void run() { - - FileLogger[] logs = (FileLogger[]) loggers.toArray(new FileLogger[0]); - - ArrayList archives = new ArrayList(); - - for (int i = 0; i < logs.length; i++) { - try { - File archive = logs[i].rotateLogFile(); - if (archive != null) { - archives.add(archive); - } - } catch (IOException io) { - System.err.println("Error rotating log " + logs[i].getName() + ": " + - io.toString()); - } - } - - // reduce thread priority for zipping - setPriority(MIN_PRIORITY); - Iterator it = archives.iterator(); - while (it.hasNext()) { - gzip((File) it.next()); - } - - } - - } - - /** - * Utility thread class to gzip a file in a separate thread - */ - static class FileGZipper extends Thread { - - File file; - - FileGZipper(File file) { - this.file = file; - } - - public void run() { - // reduce thread priority for zipping - setPriority(MIN_PRIORITY); - gzip(file); - } - } - }