Browse Source

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

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

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

@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0 * The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with * (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at * the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -21,21 +21,24 @@ 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
underlying file is rolled over at a user chosen frequency. underlying file is rolled over at a user chosen frequency.
DailyRollingFileAppender has been observed to exhibit DailyRollingFileAppender has been observed to exhibit
synchronization issues and data loss. The log4j extras synchronization issues and data loss. The log4j extras
companion includes alternatives which should be considered companion includes alternatives which should be considered
for new deployments and which are discussed in the documentation for new deployments and which are discussed in the documentation
@ -158,15 +161,17 @@ 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
example, if the rollover period is one hour, the log file will be example, if the rollover period is one hour, the log file will be
renamed to the value of "scheduledFilename" at the beginning of renamed to the value of "scheduledFilename" at the beginning of
the next hour. the next hour.
The precise time when a rollover occurs depends on logging The precise time when a rollover occurs depends on logging
activity. activity.
*/ */
private String scheduledFilename; private String scheduledFilename;
@ -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+"].");
@ -372,7 +405,7 @@ public class DailyRollingFileAppender extends FileAppender {
/** /**
* RollingCalendar is a helper class to DailyRollingFileAppender. * RollingCalendar is a helper class to DailyRollingFileAppender.
* Given a periodicity type and the current time, it computes the * Given a periodicity type and the current time, it computes the
* start of the next interval. * start of the next interval.
* */ * */
class RollingCalendar extends GregorianCalendar { class RollingCalendar extends GregorianCalendar {
private static final long serialVersionUID = -3560331770601814177L; private static final long serialVersionUID = -3560331770601814177L;
@ -381,11 +414,11 @@ class RollingCalendar extends GregorianCalendar {
RollingCalendar() { RollingCalendar() {
super(); super();
} }
RollingCalendar(TimeZone tz, Locale locale) { RollingCalendar(TimeZone tz, Locale locale) {
super(tz, locale); super(tz, locale);
} }
void setType(int type) { void setType(int type) {
this.type = type; this.type = type;

Loading…
Cancel
Save