diff --git a/fine-log4j/src/main/java/com/fr/third/apache/log4j/DailyRollingFileAppender.java b/fine-log4j/src/main/java/com/fr/third/apache/log4j/DailyRollingFileAppender.java index ab7708297..a2ec34039 100644 --- a/fine-log4j/src/main/java/com/fr/third/apache/log4j/DailyRollingFileAppender.java +++ b/fine-log4j/src/main/java/com/fr/third/apache/log4j/DailyRollingFileAppender.java @@ -317,66 +317,70 @@ public class DailyRollingFileAppender extends FileAppender { // It is too early to roll over because we are still within the // bounds of the current interval. Rollover will occur once the // next interval is reached. - if (scheduledFilename.equals(datedFilename)) { - return; - } + if (scheduledFilename.equals(datedFilename)) { + return; + } + synchronized (this) { + if (scheduledFilename.equals(datedFilename)) { + return; + } - // close current file, and compress it to datedFilename - this.closeFile(); + // close current file, and compress it to datedFilename + this.closeFile(); - File target = new File(scheduledFilename); - if (target.exists()) { - target.delete(); - } + File target = new File(scheduledFilename); + if (target.exists()) { + target.delete(); + } - File file = new File(fileName); - boolean result = false; - - FileInputStream fis = null; - FileOutputStream fos = null; - GZIPOutputStream gzos = null; - try { - fis = new FileInputStream(file); - fos = new FileOutputStream(target); - gzos = new GZIPOutputStream(fos); - byte[] inbuf = new byte[8102]; - int n; - - while ((n = fis.read(inbuf)) != -1) { - gzos.write(inbuf, 0, n); - } - result = true; - } catch (Exception e){ - LogLog.error("Compress " + fileName + " to " + scheduledFilename + " failed."); - LogLog.error(e.getMessage(), e); - } finally { - if(gzos!=null) { - gzos.close(); - } - if (fis != null) { - fis.close(); - } - } + File file = new File(fileName); + boolean result = false; + + FileInputStream fis = null; + FileOutputStream fos = null; + GZIPOutputStream gzos = null; + try { + fis = new FileInputStream(file); + fos = new FileOutputStream(target); + gzos = new GZIPOutputStream(fos); + byte[] inbuf = new byte[8102]; + int n; + + while ((n = fis.read(inbuf)) != -1) { + gzos.write(inbuf, 0, n); + } + result = true; + } catch (Exception e) { + LogLog.error("Compress " + fileName + " to " + scheduledFilename + " failed."); + LogLog.error(e.getMessage(), e); + } finally { + if (gzos != null) { + gzos.close(); + } + if (fis != null) { + fis.close(); + } + } - if(result) { - if (!file.delete()) { - LogLog.debug("file delete failed, empty it."); - emptyFile(file); - } - LogLog.debug(fileName +" -> "+ scheduledFilename); - } else { - LogLog.error("Failed to rename ["+fileName+"] to ["+scheduledFilename+"]."); - } + if (result) { + if (!file.delete()) { + LogLog.debug("file delete failed, empty it."); + emptyFile(file); + } + LogLog.debug(fileName + " -> " + scheduledFilename); + } else { + LogLog.error("Failed to rename [" + fileName + "] to [" + scheduledFilename + "]."); + } - try { - // This will also close the file. This is OK since multiple - // close operations are safe. - this.setFile(fileName, true, this.bufferedIO, this.bufferSize); - } - catch(IOException e) { - errorHandler.error("setFile("+fileName+", true) call failed."); - } - scheduledFilename = datedFilename; + try { + // This will also close the file. This is OK since multiple + // close operations are safe. + this.setFile(fileName, true, this.bufferedIO, this.bufferSize); + } catch (IOException e) { + errorHandler.error("setFile(" + fileName + ", true) call failed."); + } + scheduledFilename = datedFilename; + } }