From d1a38d38527100211ff7c7a9ee64a04d78341a2f Mon Sep 17 00:00:00 2001 From: bokai Date: Mon, 16 Nov 2020 21:41:37 +0800 Subject: [PATCH 1/7] =?UTF-8?q?REPORT-42043=20fanruan.log=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=88=86=E5=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../log4j/DailyRollingFileAppender.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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 e23b4bb85..030b46d55 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 @@ -24,6 +24,7 @@ import com.fr.third.apache.log4j.spi.LoggingEvent; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; import java.io.InterruptedIOException; import java.text.SimpleDateFormat; @@ -358,7 +359,10 @@ public class DailyRollingFileAppender extends FileAppender { } if(result) { - file.delete(); + 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+"]."); @@ -375,6 +379,24 @@ public class DailyRollingFileAppender extends FileAppender { scheduledFilename = datedFilename; } + + /** + * @param file empty file + */ + private static void emptyFile(File file) { + try { + if (!file.exists()) { + file.createNewFile(); + } + FileWriter fileWriter = new FileWriter(file); + fileWriter.write(""); + fileWriter.flush(); + fileWriter.close(); + } catch (IOException e) { + LogLog.debug("empty file failed:" + e.getMessage() + e); + } + } + /** * This method differentiates DailyRollingFileAppender from its * super class. From 5f9efe605de7ced449fc58d4f15a4da25247117a Mon Sep 17 00:00:00 2001 From: bokai Date: Tue, 17 Nov 2020 10:37:22 +0800 Subject: [PATCH 2/7] =?UTF-8?q?REPORT-42043=20fanruan.log=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=88=86=E5=89=B2-=E4=BB=A3=E7=A0=81=E8=B4=A8?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/third/apache/log4j/DailyRollingFileAppender.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 030b46d55..ab7708297 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 @@ -384,14 +384,12 @@ public class DailyRollingFileAppender extends FileAppender { * @param file empty file */ private static void emptyFile(File file) { - try { + try(FileWriter fileWriter = new FileWriter(file)) { if (!file.exists()) { file.createNewFile(); } - FileWriter fileWriter = new FileWriter(file); fileWriter.write(""); fileWriter.flush(); - fileWriter.close(); } catch (IOException e) { LogLog.debug("empty file failed:" + e.getMessage() + e); } From af4fb4b14dfc88432923254276fb7758a07f897b Mon Sep 17 00:00:00 2001 From: bokai Date: Sun, 22 Nov 2020 00:08:29 +0800 Subject: [PATCH 3/7] =?UTF-8?q?REPORT-42043=20fanruan.log=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=88=86=E5=89=B2-=E5=A4=9A=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../log4j/DailyRollingFileAppender.java | 114 +++++++++--------- 1 file changed, 59 insertions(+), 55 deletions(-) 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; + } } From 9900b8bae5f07d3ca0440f6d63376c7afeed5c96 Mon Sep 17 00:00:00 2001 From: bokai Date: Sun, 22 Nov 2020 00:14:01 +0800 Subject: [PATCH 4/7] =?UTF-8?q?REPORT-42043=20fanruan.log=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=88=86=E5=89=B2-=E5=8A=A0=E4=B8=AAvolatile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/third/apache/log4j/DailyRollingFileAppender.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a2ec34039..499e336c2 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 @@ -174,7 +174,7 @@ public class DailyRollingFileAppender extends FileAppender { The precise time when a rollover occurs depends on logging activity. */ - private String scheduledFilename; + private volatile String scheduledFilename; /** The next time we estimate a rollover should occur. */ From 40690479c13859c35d42400ca34ecadc88e6ce31 Mon Sep 17 00:00:00 2001 From: bokai Date: Sun, 22 Nov 2020 01:47:09 +0800 Subject: [PATCH 5/7] =?UTF-8?q?REPORT-42043=20fanruan.log=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=88=86=E5=89=B2-=E4=BB=A3=E7=A0=81=E8=B4=A8?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../log4j/DailyRollingFileAppender.java | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) 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 499e336c2..e90cca439 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 @@ -174,7 +174,7 @@ public class DailyRollingFileAppender extends FileAppender { The precise time when a rollover occurs depends on logging activity. */ - private volatile String scheduledFilename; + private String scheduledFilename; /** The next time we estimate a rollover should occur. */ @@ -336,13 +336,9 @@ public class DailyRollingFileAppender extends FileAppender { 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); + try (FileInputStream fis =new FileInputStream(file) ; + FileOutputStream fos = new FileOutputStream(target); + GZIPOutputStream gzos = new GZIPOutputStream(fos);){ byte[] inbuf = new byte[8102]; int n; @@ -353,13 +349,6 @@ public class DailyRollingFileAppender extends FileAppender { } 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) { From f267eabbae5c21f29b956b59529739b959cbaf0d Mon Sep 17 00:00:00 2001 From: bokai Date: Wed, 18 Nov 2020 15:58:29 +0800 Subject: [PATCH 6/7] =?UTF-8?q?REPORT-42043=20fanruan.log=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=88=86=E5=89=B2-=E4=BB=A3=E7=A0=81=E8=B4=A8?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../log4j/DailyRollingFileAppender.java | 128 +++++++++--------- 1 file changed, 66 insertions(+), 62 deletions(-) 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 e90cca439..8b33443c1 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 @@ -302,75 +302,79 @@ public class DailyRollingFileAppender extends FileAppender { return TOP_OF_TROUBLE; // Deliberately head for trouble... } - /** - Rollover the current file to a new file. - */ - void rollOver() throws IOException { + /** + * Rollover the current file to a new file. + */ + void rollOver() throws IOException { - /* Compute filename, but only if datePattern is specified */ - if (datePattern == null) { - errorHandler.error("Missing DatePattern option in rollOver()."); - return; - } + /* Compute filename, but only if datePattern is specified */ + if (datePattern == null) { + errorHandler.error("Missing DatePattern option in rollOver()."); + return; + } - String datedFilename = fileName+sdf.format(now)+COMPRESS_SUFFIX; - // 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; - } - synchronized (this) { - if (scheduledFilename.equals(datedFilename)) { - return; - } + String datedFilename = fileName + sdf.format(now) + COMPRESS_SUFFIX; + // 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; + } - // 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; - - try (FileInputStream fis =new FileInputStream(file) ; - FileOutputStream fos = new FileOutputStream(target); - GZIPOutputStream 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); - } + File file = new File(fileName); + boolean result = false; - 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 (FileInputStream fis = new FileInputStream(file); + FileOutputStream fos = new FileOutputStream(target); + GZIPOutputStream gzos = new GZIPOutputStream(fos);) { + byte[] inbuf = new byte[8102]; + int n; - 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; - } - } + 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); + } + + boolean isDeleteSuccess = true; + if (result) { + isDeleteSuccess = file.delete(); + 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."); + } + if(!isDeleteSuccess){ + synchronized (this) { + if (scheduledFilename.equals(datedFilename)) { + return; + } + LogLog.debug("file delete failed, empty it."); + emptyFile(file); + scheduledFilename = datedFilename; + } + }else { + scheduledFilename = datedFilename; + } + } /** From f7195079148fb3e88e207971e4d1be21af0419a2 Mon Sep 17 00:00:00 2001 From: bokai Date: Wed, 18 Nov 2020 19:32:53 +0800 Subject: [PATCH 7/7] =?UTF-8?q?REPORT-42043=20fanruan.log=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=88=86=E5=89=B2-=E4=BB=A3=E7=A0=81=E8=B4=A8?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/third/apache/log4j/DailyRollingFileAppender.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 8b33443c1..7b4088fa0 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 @@ -330,7 +330,7 @@ public class DailyRollingFileAppender extends FileAppender { } File file = new File(fileName); - boolean result = false; + boolean isGzipSuccess = false; try (FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(target); @@ -341,14 +341,14 @@ public class DailyRollingFileAppender extends FileAppender { while ((n = fis.read(inbuf)) != -1) { gzos.write(inbuf, 0, n); } - result = true; + isGzipSuccess = true; } catch (Exception e) { LogLog.error("Compress " + fileName + " to " + scheduledFilename + " failed."); LogLog.error(e.getMessage(), e); } boolean isDeleteSuccess = true; - if (result) { + if (isGzipSuccess) { isDeleteSuccess = file.delete(); LogLog.debug(fileName + " -> " + scheduledFilename); } else {