From d1a38d38527100211ff7c7a9ee64a04d78341a2f Mon Sep 17 00:00:00 2001 From: bokai Date: Mon, 16 Nov 2020 21:41:37 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-42043=20fanruan.log=E6=97=A0=E6=B3=95?= =?UTF-8?q?=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.