Browse Source

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

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

128
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... return TOP_OF_TROUBLE; // Deliberately head for trouble...
} }
/** /**
Rollover the current file to a new file. * Rollover the current file to a new file.
*/ */
void rollOver() throws IOException { void rollOver() throws IOException {
/* Compute filename, but only if datePattern is specified */ /* Compute filename, but only if datePattern is specified */
if (datePattern == null) { if (datePattern == null) {
errorHandler.error("Missing DatePattern option in rollOver()."); errorHandler.error("Missing DatePattern option in rollOver().");
return; 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 // It is too early to roll over because we are still within the
// bounds of the current interval. Rollover will occur once the // bounds of the current interval. Rollover will occur once the
// next interval is reached. // next interval is reached.
if (scheduledFilename.equals(datedFilename)) { if (scheduledFilename.equals(datedFilename)) {
return; return;
} }
synchronized (this) {
if (scheduledFilename.equals(datedFilename)) {
return;
}
// close current file, and compress it to datedFilename // close current file, and compress it to datedFilename
this.closeFile(); this.closeFile();
File target = new File(scheduledFilename); File target = new File(scheduledFilename);
if (target.exists()) { if (target.exists()) {
target.delete(); target.delete();
} }
File file = new File(fileName); File file = new File(fileName);
boolean result = false; 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);
}
if (result) { try (FileInputStream fis = new FileInputStream(file);
if (!file.delete()) { FileOutputStream fos = new FileOutputStream(target);
LogLog.debug("file delete failed, empty it."); GZIPOutputStream gzos = new GZIPOutputStream(fos);) {
emptyFile(file); byte[] inbuf = new byte[8102];
} int n;
LogLog.debug(fileName + " -> " + scheduledFilename);
} else {
LogLog.error("Failed to rename [" + fileName + "] to [" + scheduledFilename + "].");
}
try { while ((n = fis.read(inbuf)) != -1) {
// This will also close the file. This is OK since multiple gzos.write(inbuf, 0, n);
// close operations are safe. }
this.setFile(fileName, true, this.bufferedIO, this.bufferSize); result = true;
} catch (IOException e) { } catch (Exception e) {
errorHandler.error("setFile(" + fileName + ", true) call failed."); LogLog.error("Compress " + fileName + " to " + scheduledFilename + " failed.");
} LogLog.error(e.getMessage(), e);
scheduledFilename = datedFilename; }
}
} 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;
}
}
/** /**

Loading…
Cancel
Save