⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compositerollingappender.java

📁 实现在电脑平台上控制短信modem或手机进行通讯的jar包和相关实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.apache.log4j;/* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software * License version 1.1, a copy of which has been included with this * distribution in the LICENSE.APL file. */import org.apache.log4j.RollingCalendar;import org.apache.log4j.helpers.OptionConverter;import org.apache.log4j.helpers.QuietWriter;import org.apache.log4j.helpers.CountingQuietWriter;import org.apache.log4j.helpers.LogLog;import org.apache.log4j.spi.LoggingEvent;import java.util.Date;import java.io.IOException;import java.io.Writer;import java.text.SimpleDateFormat;import java.io.File;import java.io.FilenameFilter;/** *  <p>CompositeRollingAppender combines RollingFileAppender and DailyRollingFileAppender<br> *  It can function as either or do both at the same time (making size *  based rolling files like RollingFileAppender until a data/time boundary *  is crossed at which time it rolls all of those files as per the DailyRollingFileAppender) *  based on the setting for <code>rollingStyle</code>.<br> *  <br> *  To use CompositeRollingAppender to roll log files as they reach a certain *  size (like RollingFileAppender), set rollingStyle=1 (@see config.size)<br> *  To use CompositeRollingAppender to roll log files at certain time intervals *  (daily for example), set rollingStyle=2 and a datePattern (@see config.time)<br> *  To have CompositeRollingAppender roll log files at a certain size AND rename those *  according to time intervals, set rollingStyle=3 (@see config.composite)<br> * *  <p>A of few additional optional features have been added:<br> *  -- Attach date pattern for current log file (@see staticLogFileName)<br> *  -- Backup number increments for newer files (@see countDirection)<br> *  -- Infinite number of backups by file size (@see maxSizeRollBackups)<br> *  <br> *  <p>A few notes and warnings:  For large or infinite number of backups *  countDirection > 0 is highly recommended, with staticLogFileName = false if *  time based rolling is also used -- this will reduce the number of file renamings *  to few or none.  Changing staticLogFileName or countDirection without clearing *  the directory could have nasty side effects.  If Date/Time based rolling *  is enabled, CompositeRollingAppender will attempt to roll existing files *  in the directory without a date/time tag based on the last modified date *  of the base log files last modification.<br> *  <br> *  <p>A maximum number of backups based on date/time boundries would be nice *  but is not yet implemented.<br> * *  @author Kevin Steppe *  @author Heinz Richter *  @author Eirik Lygre *  @author Ceki G&uuml;lc&uuml; */public class CompositeRollingAppender extends org.apache.log4j.FileAppender{	// The code assumes that the following 'time' 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;	/** Style of rolling to use */	static final int BY_SIZE = 1;	static final int BY_DATE = 2;	static final int BY_COMPOSITE = 3;	//Not currently used	static final String S_BY_SIZE = "Size";	static final String S_BY_DATE = "Date";	static final String S_BY_COMPOSITE = "Composite";	/**	 The date pattern. By default, the pattern is set to	 "'.'yyyy-MM-dd" meaning daily rollover.	*/	private String datePattern = "'.'yyyy-MM-dd";	/**	 The actual formatted filename that is currently being written to	     or will be the file transferred to on roll over		 (based on staticLogFileName). */	private String scheduledFilename = null;	/** The timestamp when we shall next recompute the filename. */	private long nextCheck = System.currentTimeMillis () - 1;	/** Holds date of last roll over */	Date now = new Date();	SimpleDateFormat sdf;	/** Helper class to determine next rollover time */	RollingCalendar rc = new RollingCalendar();	/** Current period for roll overs */	int checkPeriod = TOP_OF_TROUBLE;	/**	 The default maximum file size is 10MB. */	protected long maxFileSize = 10*1024*1024;	/**	 There is zero backup files by default. */	protected int maxSizeRollBackups = 0;	/** How many sized based backups have been made so far */	protected int curSizeRollBackups = 0;	/** not yet implemented */	protected int maxTimeRollBackups = -1;	protected int curTimeRollBackups = 0;	/** By default newer files have lower numbers. (countDirection < 0)	 *  ie. log.1 is most recent, log.5 is the 5th backup, etc...	 *  countDirection > 0 does the opposite ie.	 *  log.1 is the first backup made, log.5 is the 5th backup made, etc.	 *  For infinite backups use countDirection > 0 to reduce rollOver costs.	 */	protected int countDirection = -1;	/** Style of rolling to Use.  BY_SIZE (1), BY_DATE(2), BY COMPOSITE(3) */	protected int rollingStyle = BY_COMPOSITE;	protected boolean rollDate = true;	protected boolean rollSize = true;	/** By default file.log is always the current file.  Optionally	 *  file.log.yyyy-mm-dd for current formated datePattern can by the currently	 *  logging file (or file.log.curSizeRollBackup or even	 *  file.log.yyyy-mm-dd.curSizeRollBackup) This will make time based roll	 *  overs with a large number of backups much faster -- it won't have to	 *  rename all the backups!	 */	protected boolean staticLogFileName = true;	/** FileName provided in configuration.  Used for rolling properly */	protected String baseFileName;    /** The default constructor does nothing. */	public CompositeRollingAppender()  {    }	/**	 Instantiate a <code>CompositeRollingAppender</code> and open the	 file designated by <code>filename</code>. The opened filename will	 become the ouput destination for this appender.	*/	public CompositeRollingAppender (Layout layout, String filename,				   String datePattern) throws IOException {	    this(layout, filename, datePattern, true);	}	/**	 Instantiate a CompositeRollingAppender and open the file designated by	 <code>filename</code>. The opened filename will become the ouput	 destination for this appender.	 <p>If the <code>append</code> parameter is true, the file will be	 appended to. Otherwise, the file desginated by	 <code>filename</code> will be truncated before being opened.	*/	public CompositeRollingAppender(Layout layout, String filename, boolean append)									  throws IOException {	    super(layout, filename, append);	}	/**	 Instantiate a CompositeRollingAppender and open the file designated by	 <code>filename</code>. The opened filename will become the ouput	 destination for this appender.	*/	public CompositeRollingAppender (Layout layout, String filename,				   String datePattern, boolean append) throws IOException {	    super(layout, filename, append);	    this.datePattern = datePattern;		activateOptions();	}	/**	 Instantiate a CompositeRollingAppender and open the file designated by	 <code>filename</code>. The opened filename will become the output	 destination for this appender.	 <p>The file will be appended to.  DatePattern is default.	*/	public CompositeRollingAppender(Layout layout, String filename) throws IOException {	    super(layout, filename);	}	/**	 The <b>DatePattern</b> takes a string in the same format as	 expected by {@link SimpleDateFormat}. This options determines the	 rollover schedule.	*/	public void setDatePattern(String pattern) {	    datePattern = pattern;	}	/** Returns the value of the <b>DatePattern</b> option. */	public String getDatePattern() {	    return datePattern;	}	/**	 Returns the value of the <b>maxSizeRollBackups</b> option.	*/	public int getMaxSizeRollBackups() {	    return maxSizeRollBackups;	}	/**	 Get the maximum size that the output file is allowed to reach	 before being rolled over to backup files.	 @since 1.1	*/	public long getMaximumFileSize() {		return maxFileSize;	}	/**	 <p>Set the maximum number of backup files to keep around based on file size.	 <p>The <b>MaxSizeRollBackups</b> option determines how many backup	 files are kept before the oldest is erased. This option takes	 an integer value. If set to zero, then there will be no	 backup files and the log file will be truncated when it reaches	 <code>MaxFileSize</code>.  If a negative number is supplied then	 no deletions will be made.  Note that this could result in	 very slow performance as a large number of files are rolled over unless	 {@link #setCountDirection} up is used.	 <p>The maximum applys to -each- time based group of files and -not- the total.	 Using a daily roll the maximum total files would be (#days run) * (maxSizeRollBackups)	*/	public void setMaxSizeRollBackups(int maxBackups) {	    maxSizeRollBackups = maxBackups;	}	/**	 Set the maximum size that the output file is allowed to reach	 before being rolled over to backup files.	 <p>This method is equivalent to {@link #setMaxFileSize} except	 that it is required for differentiating the setter taking a	 <code>long</code> argument from the setter taking a	 <code>String</code> argument by the JavaBeans {@link	 java.beans.Introspector Introspector}.	 @see #setMaxFileSize(String)	*/	public void setMaxFileSize(long maxFileSize) {	   this.maxFileSize = maxFileSize;	}	/**	 Set the maximum size that the output file is allowed to reach	 before being rolled over to backup files.	 <p>This method is equivalent to {@link #setMaxFileSize} except	 that it is required for differentiating the setter taking a	 <code>long</code> argument from the setter taking a	 <code>String</code> argument by the JavaBeans {@link	 java.beans.Introspector Introspector}.	 @see #setMaxFileSize(String)	*/	public void setMaximumFileSize(long maxFileSize) {		this.maxFileSize = maxFileSize;	}	/**	 Set the maximum size that the output file is allowed to reach	 before being rolled over to backup files.	 <p>In configuration files, the <b>MaxFileSize</b> option takes an	 long integer in the range 0 - 2^63. You can specify the value	 with the suffixes "KB", "MB" or "GB" so that the integer is	 interpreted being expressed respectively in kilobytes, megabytes	 or gigabytes. For example, the value "10KB" will be interpreted	 as 10240.	*/	public void setMaxFileSize(String value) {	    maxFileSize = OptionConverter.toFileSize(value, maxFileSize + 1);	}	protected void setQWForFiles(Writer writer) {	    qw = new CountingQuietWriter(writer, errorHandler);	}	//Taken verbatum from DailyRollingFileAppender	int computeCheckPeriod() {		RollingCalendar c = new RollingCalendar();		// set sate to 1970-01-01 00:00:00 GMT		Date epoch = new Date(0);		if(datePattern != null) {			for(int i = TOP_OF_MINUTE; i <= TOP_OF_MONTH; i++) {				String r0 = sdf.format(epoch);				c.setType(i);				Date next = new Date(c.getNextCheckMillis(epoch));				String r1 = sdf.format(next);				//LogLog.debug("Type = "+i+", r0 = "+r0+", r1 = "+r1);				if(r0 != null && r1 != null && !r0.equals(r1)) {					return i;				}			}		}		return TOP_OF_TROUBLE; // Deliberately head for trouble...	}	//Now for the new stuff	/**	 * Handles append time behavior for CompositeRollingAppender.  This checks	 * if a roll over either by date (checked first) or time (checked second)	 * is need and then appends to the file last.	*/	protected void subAppend(LoggingEvent event) {		if (rollDate) {			long n = System.currentTimeMillis();			if (n >= nextCheck) {				now.setTime(n);				nextCheck = rc.getNextCheckMillis(now);				rollOverTime();			}		}		if (rollSize) {			if ((fileName != null) && ((CountingQuietWriter) qw).getCount() >= maxFileSize) {			    rollOverSize();			}		}		super.subAppend(event);	}	public void setFile(String file)	{		baseFileName = file.trim();		fileName = file.trim();	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -