Browse Source

Merge pull request #1257 in CORE/base-third from bugfix/10.0 to feature/10.0

* commit '9af62acbd8b371e7296601c1fd7087b0d81705dd':
  REPORT-42043 fanruan.log无法分割-代码质量
  REPORT-42043 fanruan.log无法分割-代码质量
  REPORT-42043 fanruan.log无法分割-代码质量
  REPORT-42043 fanruan.log无法分割-加个volatile
  REPORT-42043 fanruan.log无法分割-多线程问题
  REPORT-42043 fanruan.log无法分割-代码质量
  REPORT-42043 fanruan.log无法分割
research/11.0
superman 4 years ago
parent
commit
ec8845ab2f
  1. 139
      fine-log4j/src/main/java/com/fr/third/apache/log4j/DailyRollingFileAppender.java

139
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.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InterruptedIOException; import java.io.InterruptedIOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -301,79 +302,95 @@ 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;
} }
// 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 isGzipSuccess = false;
FileInputStream fis = null; try (FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = null; FileOutputStream fos = new FileOutputStream(target);
GZIPOutputStream gzos = null; GZIPOutputStream gzos = new GZIPOutputStream(fos);) {
try { byte[] inbuf = new byte[8102];
fis = new FileInputStream(file); int n;
fos = new FileOutputStream(target);
gzos = new GZIPOutputStream(fos); while ((n = fis.read(inbuf)) != -1) {
byte[] inbuf = new byte[8102]; gzos.write(inbuf, 0, n);
int n; }
isGzipSuccess = true;
while ((n = fis.read(inbuf)) != -1) { } catch (Exception e) {
gzos.write(inbuf, 0, n); LogLog.error("Compress " + fileName + " to " + scheduledFilename + " failed.");
LogLog.error(e.getMessage(), e);
} }
result = true;
} catch (Exception e){ boolean isDeleteSuccess = true;
LogLog.error("Compress " + fileName + " to " + scheduledFilename + " failed."); if (isGzipSuccess) {
LogLog.error(e.getMessage(), e); isDeleteSuccess = file.delete();
} finally { LogLog.debug(fileName + " -> " + scheduledFilename);
if(gzos!=null) { } else {
gzos.close(); 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 (fis != null) { if(!isDeleteSuccess){
fis.close(); synchronized (this) {
if (scheduledFilename.equals(datedFilename)) {
return;
}
LogLog.debug("file delete failed, empty it.");
emptyFile(file);
scheduledFilename = datedFilename;
}
}else {
scheduledFilename = datedFilename;
} }
} }
if(result) {
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 * @param file empty file
// close operations are safe. */
this.setFile(fileName, true, this.bufferedIO, this.bufferSize); private static void emptyFile(File file) {
} try(FileWriter fileWriter = new FileWriter(file)) {
catch(IOException e) { if (!file.exists()) {
errorHandler.error("setFile("+fileName+", true) call failed."); file.createNewFile();
}
fileWriter.write("");
fileWriter.flush();
} catch (IOException e) {
LogLog.debug("empty file failed:" + e.getMessage() + e);
}
} }
scheduledFilename = datedFilename;
}
/** /**
* This method differentiates DailyRollingFileAppender from its * This method differentiates DailyRollingFileAppender from its

Loading…
Cancel
Save