Browse Source

REPORT-42043 fanruan.log无法分割-多线程问题

bokai 4 years ago
parent
commit
af4fb4b14d
  1. 114
      fine-log4j/src/main/java/com/fr/third/apache/log4j/DailyRollingFileAppender.java

114
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 // 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;
FileInputStream fis = null; FileInputStream fis = null;
FileOutputStream fos = null; FileOutputStream fos = null;
GZIPOutputStream gzos = null; GZIPOutputStream gzos = null;
try { try {
fis = new FileInputStream(file); fis = new FileInputStream(file);
fos = new FileOutputStream(target); fos = new FileOutputStream(target);
gzos = new GZIPOutputStream(fos); gzos = new GZIPOutputStream(fos);
byte[] inbuf = new byte[8102]; byte[] inbuf = new byte[8102];
int n; int n;
while ((n = fis.read(inbuf)) != -1) { while ((n = fis.read(inbuf)) != -1) {
gzos.write(inbuf, 0, n); gzos.write(inbuf, 0, n);
} }
result = true; result = true;
} catch (Exception e){ } catch (Exception e) {
LogLog.error("Compress " + fileName + " to " + scheduledFilename + " failed."); LogLog.error("Compress " + fileName + " to " + scheduledFilename + " failed.");
LogLog.error(e.getMessage(), e); LogLog.error(e.getMessage(), e);
} finally { } finally {
if(gzos!=null) { if (gzos != null) {
gzos.close(); gzos.close();
} }
if (fis != null) { if (fis != null) {
fis.close(); fis.close();
} }
} }
if(result) { if (result) {
if (!file.delete()) { if (!file.delete()) {
LogLog.debug("file delete failed, empty it."); LogLog.debug("file delete failed, empty it.");
emptyFile(file); emptyFile(file);
} }
LogLog.debug(fileName +" -> "+ scheduledFilename); LogLog.debug(fileName + " -> " + scheduledFilename);
} else { } else {
LogLog.error("Failed to rename ["+fileName+"] to ["+scheduledFilename+"]."); LogLog.error("Failed to rename [" + fileName + "] to [" + scheduledFilename + "].");
} }
try { try {
// This will also close the file. This is OK since multiple // This will also close the file. This is OK since multiple
// close operations are safe. // close operations are safe.
this.setFile(fileName, true, this.bufferedIO, this.bufferSize); this.setFile(fileName, true, this.bufferedIO, this.bufferSize);
} } catch (IOException e) {
catch(IOException e) { errorHandler.error("setFile(" + fileName + ", true) call failed.");
errorHandler.error("setFile("+fileName+", true) call failed."); }
} scheduledFilename = datedFilename;
scheduledFilename = datedFilename; }
} }

Loading…
Cancel
Save