根据DailyRollingFileAppender和RollingFileAppender改编,支持按日期和文件大小分割日志。
源文件:
-
package com.bao.logging;
-
-
import java.io.File;
-
import java.io.IOException;
-
import java.io.Writer;
-
import java.text.SimpleDateFormat;
-
import java.util.Calendar;
-
import java.util.Date;
-
import java.util.GregorianCalendar;
-
import java.util.Locale;
-
import java.util.TimeZone;
-
-
import org.apache.log4j.FileAppender;
-
import org.apache.log4j.Layout;
-
import org.apache.log4j.helpers.CountingQuietWriter;
-
import org.apache.log4j.helpers.LogLog;
-
import org.apache.log4j.helpers.OptionConverter;
-
import org.apache.log4j.spi.LoggingEvent;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public class MyDailyRollingFileAppender extends FileAppender {
-
-
-
-
static final int TOP_OF_TROUBLE = -1;
-
static final int TOP_OF_MINUTE = 0;
-
static final int TOP_OF_HOUR = 1;
-
static final int HALF_DAY = 2;
-
static final int TOP_OF_DAY = 3;
-
static final int TOP_OF_WEEK = 4;
-
static final int TOP_OF_MONTH = 5;
-
-
-
-
-
protected long maxFileSize = 10 * 1024 * 1024;
-
-
-
-
-
protected int maxBackupIndex = 1;
-
-
-
-
-
-
private String datePattern = "‘.‘yyyy-MM-dd";
-
-
-
-
-
-
-
-
-
-
private String scheduledFilename;
-
-
-
-
-
private long nextCheck = System.currentTimeMillis() - 1;
-
-
Date now = new Date();
-
-
SimpleDateFormat sdf;
-
-
RollingCalendar rc = new RollingCalendar();
-
-
int checkPeriod = TOP_OF_TROUBLE;
-
-
-
static final TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");
-
-
-
-
-
public MyDailyRollingFileAppender() {
-
}
-
-
-
-
-
-
-
public MyDailyRollingFileAppender(Layout layout, String filename,
-
String datePattern) throws IOException {
-
super(layout, filename, true);
-
this.datePattern = datePattern;
-
activateOptions();
-
}
-
-
-
-
-
-
-
-
public long getMaximumFileSize() {
-
return maxFileSize;
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public void setMaximumFileSize(long maxFileSize) {
-
this.maxFileSize = maxFileSize;
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
public void setMaxFileSize(String value) {
-
maxFileSize = OptionConverter.toFileSize(value, maxFileSize + 1);
-
}
-
-
-
-
-
public int getMaxBackupIndex() {
-
return maxBackupIndex;
-
}
-
-
-
-
-
-
-
-
-
-
-
public void setMaxBackupIndex(int maxBackups) {
-
this.maxBackupIndex = maxBackups;
-
}
-
-
-
-
-
-
public void setDatePattern(String pattern) {
-
datePattern = pattern;
-
}
-
-
-
public String getDatePattern() {
-
return datePattern;
-
}
-
-
public void activateOptions() {
-
super.activateOptions();
-
if (datePattern != null && fileName != null) {
-
now.setTime(System.currentTimeMillis());
-
sdf = new SimpleDateFormat(datePattern);
-
int type = computeCheckPeriod();
-
printPeriodicity(type);
-
rc.setType(type);
-
File file = new File(fileName);
-
scheduledFilename = fileName
-
+ sdf.format(new Date(file.lastModified()));
-
-
} else {
-
LogLog.error("Either File or DatePattern options are not set for appender ["
-
+ name + "].");
-
}
-
}
-
-
void printPeriodicity(int type) {
-
switch (type) {
-
case TOP_OF_MINUTE:
-
LogLog.debug("Appender [" + name + "] to be rolled every minute.");
-
break;
-
case TOP_OF_HOUR:
-
LogLog.debug("Appender [" + name
-
+ "] to be rolled on top of every hour.");
-
break;
-
case HALF_DAY:
-
LogLog.debug("Appender [" + name
-
+ "] to be rolled at midday and midnight.");
-
break;
-
case TOP_OF_DAY:
-
LogLog.debug("Appender [" + name + "] to be rolled at midnight.");
-
break;
-
case TOP_OF_WEEK:
-
LogLog.debug("Appender [" + name
-
+ "] to be rolled at start of week.");
-
break;
-
case TOP_OF_MONTH:
-
LogLog.debug("Appender [" + name
-
+ "] to be rolled at start of every month.");
-
break;
-
default:
-
LogLog.warn("Unknown periodicity for appender [" + name + "].");
-
}
-
}
-
-
-
-
-
-
-
-
-
-
-
int computeCheckPeriod() {
-
RollingCalendar rollingCalendar = new RollingCalendar(gmtTimeZone,
-
Locale.ENGLISH);
-
-
Date epoch = new Date(0);
-
if (datePattern != null) {
-
for (int i = TOP_OF_MINUTE; i <= TOP_OF_MONTH; i++) {
-
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
-
datePattern);
-
simpleDateFormat.setTimeZone(gmtTimeZone);
-
-
String r0 = simpleDateFormat.format(epoch);
-
rollingCalendar.setType(i);
-
Date next = new Date(rollingCalendar.getNextCheckMillis(epoch));
-
String r1 = simpleDateFormat.format(next);
-
-
if (r0 != null && r1 != null && !r0.equals(r1)) {
-
return i;
-
}
-
}
-
}
-
return TOP_OF_TROUBLE;
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public
-
void sizeRollOver() {
-
File target;
-
File file;
-
-
LogLog.debug("rolling over count="
-
+ ((CountingQuietWriter) qw).getCount());
-
LogLog.debug("maxBackupIndex=" + maxBackupIndex);
-
-
String datedFilename = fileName + sdf.format(now);
-
-
if (maxBackupIndex > 0) {
-
-
file = new File(datedFilename + ‘.‘ + maxBackupIndex);
-
if (file.exists())
-
file.delete();
-
-
-
-
for (int i = maxBackupIndex - 1; i >= 1; i--) {
-
file = new File(datedFilename + "." + i);
-
if (file.exists()) {
-
target = new File(datedFilename + ‘.‘ + (i + 1));
-
LogLog.debug("Renaming file " + file + " to " + target);
-
file.renameTo(target);
-
}
-
}
-
-
-
target = new File(datedFilename + "." + 1);
-
-
this.closeFile();
-
-
file = new File(fileName);
-
LogLog.debug("Renaming file " + file + " to " + target);
-
file.renameTo(target);
-
}else if (maxBackupIndex < 0){
-
-
for (int i = 1; i < Integer.MAX_VALUE; i++) {
-
target = new File(datedFilename + "." + i);
-
if (! target.exists()) {
-
this.closeFile();
-
file = new File(fileName);
-
file.renameTo(target);
-
LogLog.debug("Renaming file " + file + " to " + target);
-
break;
-
}
-
}
-
}
-
-
try {
-
-
-
this.setFile(fileName, false, bufferedIO, bufferSize);
-
} catch (IOException e) {
-
LogLog.error("setFile(" + fileName + ", false) call failed.", e);
-
}
-
scheduledFilename = datedFilename;
-
}
-
-
public synchronized void setFile(String fileName, boolean append,
-
boolean bufferedIO, int bufferSize) throws IOException {
-
super.setFile(fileName, append, this.bufferedIO, this.bufferSize);
-
if (append) {
-
File f = new File(fileName);
-
((CountingQuietWriter) qw).setCount(f.length());
-
}
-
}
-
-
protected void setQWForFiles(Writer writer) {
-
this.qw = new CountingQuietWriter(writer, errorHandler);
-
}
-
-
-
-
-
void timeRollOver() throws IOException {
-
-
-
if (datePattern == null) {
-
errorHandler.error("Missing DatePattern option in rollOver().");
-
return;
-
}
-
-
String datedFilename = fileName + sdf.format(now);
-
-
-
-
if (scheduledFilename.equals(datedFilename)) {
-
return;
-
}
-
-
-
this.closeFile();
-
-
File target = new File(scheduledFilename);
-
if (target.exists()) {
-
target.delete();
-
}
-
-
File file = new File(fileName);
-
boolean result = file.renameTo(target);
-
if (result) {
-
LogLog.debug(fileName + " -> " + scheduledFilename);
-
} else {
-
LogLog.error("Failed to rename [" + fileName + "] to ["
-
+ scheduledFilename + "].");
-
}
-
-
try {
-
-
-
super.setFile(fileName, false, this.bufferedIO, this.bufferSize);
-
} catch (IOException e) {
-
errorHandler.error("setFile(" + fileName + ", false) call failed.");
-
}
-
scheduledFilename = datedFilename;
-
}
-
-
-
-
-
-
-
-
-
-
protected void subAppend(LoggingEvent event) {
-
long n = System.currentTimeMillis();
-
-
if (n >= nextCheck) {
-
now.setTime(n);
-
nextCheck = rc.getNextCheckMillis(now);
-
try {
-
timeRollOver();
-
} catch (IOException ioe) {
-
LogLog.error("rollOver() failed.", ioe);
-
}
-
} else if ((fileName != null)
-
&& ((CountingQuietWriter) qw).getCount() >= maxFileSize) {
-
sizeRollOver();
-
}
-
super.subAppend(event);
-
-
}
-
}
-
-
-
-
-
-
-
class RollingCalendar extends GregorianCalendar {
-
-
int type = MyDailyRollingFileAppender.TOP_OF_TROUBLE;
-
-
RollingCalendar() {
-
super();
-
}
-
-
RollingCalendar(TimeZone tz, Locale locale) {
-
super(tz, locale);
-
}
-
-
void setType(int type) {
-
this.type = type;
-
}
-
-
public long getNextCheckMillis(Date now) {
-
return getNextCheckDate(now).getTime();
-
}
-
-
public Date getNextCheckDate(Date now) {
-
this.setTime(now);
-
-
switch (type) {
-
case MyDailyRollingFileAppender.TOP_OF_MINUTE:
-
this.set(Calendar.SECOND, 0);
-
this.set(Calendar.MILLISECOND, 0);
-
this.add(Calendar.MINUTE, 1);
-
break;
-
case MyDailyRollingFileAppender.TOP_OF_HOUR:
-
this.set(Calendar.MINUTE, 0);
-
this.set(Calendar.SECOND, 0);
-
this.set(Calendar.MILLISECOND, 0);
-
this.add(Calendar.HOUR_OF_DAY, 1);
-
break;
-
case MyDailyRollingFileAppender.HALF_DAY:
-
this.set(Calendar.MINUTE, 0);
-
this.set(Calendar.SECOND, 0);
-
this.set(Calendar.MILLISECOND, 0);
-
int hour = get(Calendar.HOUR_OF_DAY);
-
if (hour < 12) {
-
this.set(Calendar.HOUR_OF_DAY, 12);
-
} else {
-
this.set(Calendar.HOUR_OF_DAY, 0);
-
this.add(Calendar.DAY_OF_MONTH, 1);
-
}
-
break;
-
case MyDailyRollingFileAppender.TOP_OF_DAY:
-
this.set(Calendar.HOUR_OF_DAY, 0);
-
this.set(Calendar.MINUTE, 0);
-
this.set(Calendar.SECOND, 0);
-
this.set(Calendar.MILLISECOND, 0);
-
this.add(Calendar.DATE, 1);
-
break;
-
case MyDailyRollingFileAppender.TOP_OF_WEEK:
-
this.set(Calendar.DAY_OF_WEEK, getFirstDayOfWeek());
-
this.set(Calendar.HOUR_OF_DAY, 0);
-
this.set(Calendar.SECOND, 0);
-
this.set(Calendar.MILLISECOND, 0);
-
this.add(Calendar.WEEK_OF_YEAR, 1);
-
break;
-
case MyDailyRollingFileAppender.TOP_OF_MONTH:
-
this.set(Calendar.DATE, 1);
-
this.set(Calendar.HOUR_OF_DAY, 0);
-
this.set(Calendar.SECOND, 0);
-
this.set(Calendar.MILLISECOND, 0);
-
this.add(Calendar.MONTH, 1);
-
break;
-
default:
-
throw new IllegalStateException("Unknown periodicity type.");
-
}
-
return getTime();
-
}
-
}
用法:
-
<appender name="PROJECT" class="com.bao.logging.MyDailyRollingFileAppender">
-
<param name="file" value="e:/test.log"/>
-
<param name="DatePattern" value="‘.‘yyyy-MM-dd‘.log‘" />
-
<param name="append" value="true"/>
-
<param name="MaxFileSize" value="500MB"/>
-
<param name="MaxBackupIndex" value="20"/>
-
<!-- <param name="MaxBackupIndex" value="-1"/> --><!-- 无限的文件数量,index顺序按时间顺序递增 -->
-
<param name="encoding" value="UTF-8"/>
-
<param name="threshold" value="info"/>
-
<layout class="org.apache.log4j.PatternLayout">
-
<param name="ConversionPattern" value="[%d{dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n"/>
-
</layout>
-
</appender>
[Java][log4j]支持同时按日期和文件大小分割日志,布布扣,bubuko.com
[Java][log4j]支持同时按日期和文件大小分割日志
原文:http://blog.csdn.net/szwangdf/article/details/37563307