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 "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.spi.LoggingEvent;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Locale;
import java.util.TimeZone;
import java.util.zip.GZIPOutputStream;
/**
DailyRollingFileAppender extends {@link FileAppender} so that the
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
companion includes alternatives which should be considered
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 static final String COMPRESS_SUFFIX = ".gz";
/**
The log file will be renamed to the value of the
scheduledFilename variable when the next interval is entered. For
example, if the rollover period is one hour, the log file will be
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
activity.
activity.
*/
private String scheduledFilename;
@ -227,7 +232,7 @@ public class DailyRollingFileAppender extends FileAppender {
printPeriodicity(type);
rc.setType(type);
File file = new File(fileName);
scheduledFilename = fileName+sdf.format(new Date(file.lastModified()));
scheduledFilename = fileName+sdf.format(new Date(file.lastModified()))+COMPRESS_SUFFIX;
} else {
LogLog.error("Either File or DatePattern options are not set for appender ["
@ -307,7 +312,7 @@ public class DailyRollingFileAppender extends FileAppender {
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
// bounds of the current interval. Rollover will occur once the
// next interval is reached.
@ -315,7 +320,7 @@ public class DailyRollingFileAppender extends FileAppender {
return;
}
// close current file, and rename it to datedFilename
// close current file, and compress it to datedFilename
this.closeFile();
File target = new File(scheduledFilename);
@ -324,8 +329,36 @@ public class DailyRollingFileAppender extends FileAppender {
}
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) {
file.delete();
LogLog.debug(fileName +" -> "+ scheduledFilename);
} else {
LogLog.error("Failed to rename ["+fileName+"] to ["+scheduledFilename+"].");
@ -372,7 +405,7 @@ public class DailyRollingFileAppender extends FileAppender {
/**
* RollingCalendar is a helper class to DailyRollingFileAppender.
* 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 {
private static final long serialVersionUID = -3560331770601814177L;
@ -381,11 +414,11 @@ class RollingCalendar extends GregorianCalendar {
RollingCalendar() {
super();
}
}
RollingCalendar(TimeZone tz, Locale locale) {
super(tz, locale);
}
}
void setType(int type) {
this.type = type;

Loading…
Cancel
Save