Browse Source

REPORT-42043 fanruan.log无法分割-代码质量

release/10.0
bokai 4 years ago
parent
commit
f267eabbae
  1. 28
      fine-log4j/src/main/java/com/fr/third/apache/log4j/DailyRollingFileAppender.java

28
fine-log4j/src/main/java/com/fr/third/apache/log4j/DailyRollingFileAppender.java

@ -303,7 +303,7 @@ public class DailyRollingFileAppender extends FileAppender {
}
/**
Rollover the current file to a new file.
* Rollover the current file to a new file.
*/
void rollOver() throws IOException {
@ -313,17 +313,13 @@ public class DailyRollingFileAppender extends FileAppender {
return;
}
String datedFilename = fileName+sdf.format(now)+COMPRESS_SUFFIX;
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;
}
// close current file, and compress it to datedFilename
this.closeFile();
@ -336,9 +332,9 @@ public class DailyRollingFileAppender extends FileAppender {
File file = new File(fileName);
boolean result = false;
try (FileInputStream fis =new FileInputStream(file) ;
try (FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(target);
GZIPOutputStream gzos = new GZIPOutputStream(fos);){
GZIPOutputStream gzos = new GZIPOutputStream(fos);) {
byte[] inbuf = new byte[8102];
int n;
@ -351,11 +347,9 @@ public class DailyRollingFileAppender extends FileAppender {
LogLog.error(e.getMessage(), e);
}
boolean isDeleteSuccess = true;
if (result) {
if (!file.delete()) {
LogLog.debug("file delete failed, empty it.");
emptyFile(file);
}
isDeleteSuccess = file.delete();
LogLog.debug(fileName + " -> " + scheduledFilename);
} else {
LogLog.error("Failed to rename [" + fileName + "] to [" + scheduledFilename + "].");
@ -368,6 +362,16 @@ public class DailyRollingFileAppender extends FileAppender {
} 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;
}
}

Loading…
Cancel
Save