|
|
@ -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; |
|
|
@ -302,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 { |
|
|
|
void rollOver() throws IOException { |
|
|
|
|
|
|
|
|
|
|
@ -329,36 +330,26 @@ public class DailyRollingFileAppender extends FileAppender { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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 { |
|
|
|
|
|
|
|
fis = new FileInputStream(file); |
|
|
|
|
|
|
|
fos = new FileOutputStream(target); |
|
|
|
|
|
|
|
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; |
|
|
|
isGzipSuccess = 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 { |
|
|
|
|
|
|
|
if(gzos!=null) { |
|
|
|
|
|
|
|
gzos.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (fis != null) { |
|
|
|
|
|
|
|
fis.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(result) { |
|
|
|
boolean isDeleteSuccess = true; |
|
|
|
file.delete(); |
|
|
|
if (isGzipSuccess) { |
|
|
|
|
|
|
|
isDeleteSuccess = file.delete(); |
|
|
|
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 + "]."); |
|
|
@ -368,12 +359,38 @@ public class DailyRollingFileAppender extends FileAppender { |
|
|
|
// 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."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if(!isDeleteSuccess){ |
|
|
|
|
|
|
|
synchronized (this) { |
|
|
|
|
|
|
|
if (scheduledFilename.equals(datedFilename)) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
LogLog.debug("file delete failed, empty it."); |
|
|
|
|
|
|
|
emptyFile(file); |
|
|
|
|
|
|
|
scheduledFilename = datedFilename; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}else { |
|
|
|
scheduledFilename = datedFilename; |
|
|
|
scheduledFilename = datedFilename; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @param file empty file |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private static void emptyFile(File file) { |
|
|
|
|
|
|
|
try(FileWriter fileWriter = new FileWriter(file)) { |
|
|
|
|
|
|
|
if (!file.exists()) { |
|
|
|
|
|
|
|
file.createNewFile(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
fileWriter.write(""); |
|
|
|
|
|
|
|
fileWriter.flush(); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
LogLog.debug("empty file failed:" + e.getMessage() + e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* This method differentiates DailyRollingFileAppender from its |
|
|
|
* This method differentiates DailyRollingFileAppender from its |
|
|
|