Browse Source

REPORT-27582 设计器-日志没有生成压缩文件

release/10.0
bokai 5 years ago
parent
commit
109c9a7862
  1. 47
      fine-log4j/src/com/fr/third/apache/log4j/DailyRollingFileAppender.java

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

@ -21,15 +21,18 @@ package com.fr.third.apache.log4j;
import com.fr.third.apache.log4j.helpers.LogLog; import com.fr.third.apache.log4j.helpers.LogLog;
import com.fr.third.apache.log4j.spi.LoggingEvent; import com.fr.third.apache.log4j.spi.LoggingEvent;
import java.io.IOException;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException; import java.io.InterruptedIOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone;
import java.util.zip.GZIPOutputStream;
/** /**
DailyRollingFileAppender extends {@link FileAppender} so that the DailyRollingFileAppender extends {@link FileAppender} so that the
@ -158,6 +161,8 @@ public class DailyRollingFileAppender extends FileAppender {
*/ */
private String datePattern = "'.'yyyy-MM-dd"; private String datePattern = "'.'yyyy-MM-dd";
private static final String COMPRESS_SUFFIX = ".gz";
/** /**
The log file will be renamed to the value of the The log file will be renamed to the value of the
scheduledFilename variable when the next interval is entered. For scheduledFilename variable when the next interval is entered. For
@ -227,7 +232,7 @@ public class DailyRollingFileAppender extends FileAppender {
printPeriodicity(type); printPeriodicity(type);
rc.setType(type); rc.setType(type);
File file = new File(fileName); File file = new File(fileName);
scheduledFilename = fileName+sdf.format(new Date(file.lastModified())); scheduledFilename = fileName+sdf.format(new Date(file.lastModified()))+COMPRESS_SUFFIX;
} else { } else {
LogLog.error("Either File or DatePattern options are not set for appender [" LogLog.error("Either File or DatePattern options are not set for appender ["
@ -307,7 +312,7 @@ public class DailyRollingFileAppender extends FileAppender {
return; return;
} }
String datedFilename = fileName+sdf.format(now); 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.
@ -315,7 +320,7 @@ public class DailyRollingFileAppender extends FileAppender {
return; return;
} }
// close current file, and rename 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);
@ -324,8 +329,36 @@ public class DailyRollingFileAppender extends FileAppender {
} }
File file = new File(fileName); File file = new File(fileName);
boolean result = file.renameTo(target); boolean result = false;
FileInputStream fis = null;
FileOutputStream fos = null;
GZIPOutputStream gzos = null;
try {
fis = new FileInputStream(file);
fos = new FileOutputStream(target);
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);
} finally {
if(gzos!=null) {
gzos.close();
}
if (fis != null) {
fis.close();
}
}
if(result) { if(result) {
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+"].");

Loading…
Cancel
Save