log4j DailyRollingFileAppender支持保留最近n天,并且可以对一天的文件根据大小切分...

本文介绍了如何解决log4j DailyRollingFileAppender随着时间推移积累大量日志文件的问题。作者通过自定义appender实现了仅保留最近n天日志的功能,并根据文件大小进行切分。具体实现代码中添加了MaxBackupIndex配置选项。
摘要由CSDN通过智能技术生成

log4j DailyRollingFileAppender不支持只保留最近n天的数据,时间一久导致日志文件很多,并且一天的文件有可能比较大,所以自己写了个appender,以解决这两个问题。

    具体代码实现如下:

package com.mytools.common.log4jext;

import java.io.File;
import java.io.FileFilter;
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.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.helpers.CountingQuietWriter;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;

/**
 * log4j appender扩展<br>
 * (1)按天并且只保留最近n天的 <br>
 * (2)如果一天的文件过大,可以按配置的大小将一天的文件进行切分
 * 
 * @author shaoqz
 * @since 2016-6-13
 */
public class MyDailyRollingFileAppender extends FileAppender
{

	// The code assumes that the following constants are in a increasing
	// sequence.
	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;

	/**
	 * The gmtTimeZone is used only in computeCheckPeriod() method.
	 */
	public static final TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");

	/**
	 * The default maximum file size is 10MB. , 10K for test
	 */
	protected long maxFileSize = 10 * 1024;

	/**
	 * The date pattern. By default, the pattern is set to "'.'yyyy-MM-dd" meaning daily
	 * rollover.
	 */
	private String datePattern = "'.'yyyy-MM-dd";

	/**
	 * There is one backup file by default.
	 */
	private int maxBackupIndex = 1;

	/**
	 * 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 precise time when a rollover occurs depends on logging activity.
	 */
	private String scheduledFilename;

	/**
	 * The next time we estimate a rollover should occur.
	 */
	private long nextCheck = System.currentTimeMillis() - 1;

	private Date now = new Date();

	private SimpleDateFormat sdf;

	private MyRollingCalendar rollingCalendar = new MyRollingCalendar();

	public int checkPeriod = TOP_OF_TROUBLE;

	/**
	 * The default constructor does nothing.
	 */
	public MyDailyRollingFileAppender()
	{
	}

	/**
	 * Instantiate a <code>DailyRollingFileAppender</code> and open the file designated by
	 * <code>filename</code>. The opened filename will become the ouput destination for
	 * this appender.
	 */
	public MyDailyRollingFileAppender(Layout layout, String filename, String datePattern) throws IOException
	{
		super(layout, filename, true);
		this.datePattern = datePattern;
		activateOptions();
	}

	/**
	 * The <b>DatePattern</b> take
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值