- Make sure we use an absolute path for logdir
- Make all methods that work on the logfile/writer synchronized - Increase Buffer size in GZipper thread to 8192 bytes
This commit is contained in:
parent
5bc692e923
commit
3a55c1b270
1 changed files with 11 additions and 7 deletions
|
@ -56,6 +56,9 @@ public class FileLogger extends Logger implements Log {
|
||||||
protected FileLogger(String directory, String name) {
|
protected FileLogger(String directory, String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
logdir = new File(directory);
|
logdir = new File(directory);
|
||||||
|
// make logdir have an absolute path in case it doesn't already
|
||||||
|
if (!logdir.isAbsolute())
|
||||||
|
logdir = logdir.getAbsoluteFile();
|
||||||
logfile = new File(logdir, name + ".log");
|
logfile = new File(logdir, name + ".log");
|
||||||
|
|
||||||
if (!logdir.exists()) {
|
if (!logdir.exists()) {
|
||||||
|
@ -75,7 +78,7 @@ public class FileLogger extends Logger implements Log {
|
||||||
* Open the file and get a writer to it. This will either rotate the log files
|
* Open the file and get a writer to it. This will either rotate the log files
|
||||||
* or it will return a writer that appends to an existing file.
|
* or it will return a writer that appends to an existing file.
|
||||||
*/
|
*/
|
||||||
private void openFile() {
|
private synchronized void openFile() {
|
||||||
try {
|
try {
|
||||||
if (logfile.exists() && (logfile.lastModified() < lastMidnight())) {
|
if (logfile.exists() && (logfile.lastModified() < lastMidnight())) {
|
||||||
// rotate if a log file exists and is NOT from today
|
// rotate if a log file exists and is NOT from today
|
||||||
|
@ -93,7 +96,7 @@ public class FileLogger extends Logger implements Log {
|
||||||
/**
|
/**
|
||||||
* Actually closes the file writer of a log.
|
* Actually closes the file writer of a log.
|
||||||
*/
|
*/
|
||||||
void closeFile() {
|
synchronized void closeFile() {
|
||||||
if (writer != null) {
|
if (writer != null) {
|
||||||
try {
|
try {
|
||||||
writer.close();
|
writer.close();
|
||||||
|
@ -107,14 +110,14 @@ public class FileLogger extends Logger implements Log {
|
||||||
/**
|
/**
|
||||||
* Return true if we have a file writer open
|
* Return true if we have a file writer open
|
||||||
*/
|
*/
|
||||||
boolean isOpen() {
|
synchronized boolean isOpen() {
|
||||||
return writer != null;
|
return writer != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is called by the runner thread to perform actual output.
|
* This is called by the runner thread to perform actual output.
|
||||||
*/
|
*/
|
||||||
void write() {
|
synchronized void write() {
|
||||||
if (entries.isEmpty()) {
|
if (entries.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -152,7 +155,7 @@ public class FileLogger extends Logger implements Log {
|
||||||
* Rotate log files, closing, renaming and gzipping the old file and
|
* Rotate log files, closing, renaming and gzipping the old file and
|
||||||
* start a new one.
|
* start a new one.
|
||||||
*/
|
*/
|
||||||
protected void rotateLogFile() throws IOException {
|
protected synchronized void rotateLogFile() throws IOException {
|
||||||
// if the logger is not file based do nothing.
|
// if the logger is not file based do nothing.
|
||||||
if (logfile == null) {
|
if (logfile == null) {
|
||||||
return;
|
return;
|
||||||
|
@ -263,6 +266,7 @@ public class FileLogger extends Logger implements Log {
|
||||||
class GZipper extends Thread {
|
class GZipper extends Thread {
|
||||||
File file;
|
File file;
|
||||||
File temp;
|
File temp;
|
||||||
|
final static int BUFFER_SIZE = 8192;
|
||||||
|
|
||||||
public GZipper(File file) {
|
public GZipper(File file) {
|
||||||
this.file = file;
|
this.file = file;
|
||||||
|
@ -273,10 +277,10 @@ public class FileLogger extends Logger implements Log {
|
||||||
try {
|
try {
|
||||||
GZIPOutputStream zip = new GZIPOutputStream(new FileOutputStream(temp));
|
GZIPOutputStream zip = new GZIPOutputStream(new FileOutputStream(temp));
|
||||||
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
|
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
|
||||||
byte[] b = new byte[1024];
|
byte[] b = new byte[BUFFER_SIZE];
|
||||||
int len = 0;
|
int len = 0;
|
||||||
|
|
||||||
while ((len = in.read(b, 0, 1024)) != -1) {
|
while ((len = in.read(b, 0, BUFFER_SIZE)) != -1) {
|
||||||
zip.write(b, 0, len);
|
zip.write(b, 0, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue