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
This commit is contained in:
hns 2005-06-22 08:37:25 +00:00
parent 365fab6a63
commit d743c82c89
2 changed files with 68 additions and 75 deletions

View file

@ -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());
}
}
}
}
}

View file

@ -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,29 +172,31 @@ 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);
for (int i = nloggers - 1; i >= 0; i--) {
FileLogger log = (FileLogger) loggers.get(i);
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;
while ((len = in.read(b, 0, BUFFER_SIZE)) != -1) {
zip.write(b, 0, len);
File file = log.rotateLogFile();
if (file != null) {
files.add(file);
}
zip.close();
in.close();
file.delete();
} catch (Exception e) {
System.err.println("Error gzipping " + file);
System.err.println(e.toString());
} catch (IOException io) {
System.err.println("Error rotating log " + log.getName() + ": " +
io.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);
}
}
}