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

📄 compositerollingappender.java

📁 实现在电脑平台上控制短信modem或手机进行通讯的jar包和相关实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	/**	 * Creates and opens the file for logging.  If <code>staticLogFileName</code>	 * is false then the fully qualified name is determined and used.	 */	public synchronized void setFile(String fileName, boolean append) throws IOException {		if (!staticLogFileName) {		    scheduledFilename = fileName = fileName.trim() + sdf.format(now);			if (countDirection > 0) {				scheduledFilename = fileName = fileName + '.' + (++curSizeRollBackups);			}		}		super.setFile(fileName, append);		if(append) {		  File f = new File(fileName);		  ((CountingQuietWriter) qw).setCount(f.length());		}	}	public int getCountDirection() {		return countDirection;	}	public void setCountDirection(int direction) {		countDirection = direction;	}	public int getRollingStyle () {        return rollingStyle;	}	public void setRollingStyle(int style) {	    rollingStyle = style;		switch (rollingStyle) {			case BY_SIZE:				 rollDate = false;				 rollSize = true;				 break;			case BY_DATE:				 rollDate = true;				 rollSize = false;				 break;			case BY_COMPOSITE:				 rollDate = true;				 rollSize = true;				 break;			default:				errorHandler.error("Invalid rolling Style, use 1 (by size only), 2 (by date only) or 3 (both)");		}	}/*	public void setRollingStyle(String style) {		if (style == S_BY_SIZE) {		    rollingStyle = BY_SIZE;		}		else if (style == S_BY_DATE) {		    rollingStyle = BY_DATE;		}		else if (style == S_BY_COMPOSITE) {			rollingStyle = BY_COMPOSITE;		}	}*/	public boolean getStaticLogFileName() {	    return staticLogFileName;	}	public void setStaticLogFileName(boolean s) {		staticLogFileName = s;	}	public void setStaticLogFileName(String value) {		setStaticLogFileName(OptionConverter.toBoolean(value, true));	}	/**	 *  Initializes based on exisiting conditions at time of <code>	 *  activateOptions</code>.  The following is done:<br>	 *  <br>	 *	A) determine curSizeRollBackups<br>	 *	B) determine curTimeRollBackups (not implemented)<br>	 *	C) initiates a roll over if needed for crossing a date boundary since	 *  the last run.	 */	protected void existingInit() {		curSizeRollBackups = 0;		curTimeRollBackups = 0;		//part A starts here		String filter;		if (staticLogFileName || !rollDate) {			filter = baseFileName + ".*";		}		else {			filter = scheduledFilename + ".*";		}		File f = new File(baseFileName);		f = f.getParentFile();		if (f == null)		   f = new File(".");		LogLog.debug("Searching for existing files in: " + f);		String[] files = f.list();		if (files != null) {			for (int i = 0; i < files.length; i++) {				if (!files[i].startsWith(baseFileName))				   continue;				int index = files[i].lastIndexOf(".");				if (staticLogFileName) {				   int endLength = files[i].length() - index;				   if (baseFileName.length() + endLength != files[i].length()) {					   //file is probably scheduledFilename + .x so I don't care					   continue;				   }				}				try {					int backup = Integer.parseInt(files[i].substring(index + 1, files[i].length()));					LogLog.debug("From file: " + files[i] + " -> " + backup);					if (backup > curSizeRollBackups)					   curSizeRollBackups = backup;				}				catch (Exception e) {					//this happens when file.log -> file.log.yyyy-mm-dd which is normal					//when staticLogFileName == false					LogLog.debug("Encountered a backup file not ending in .x " + files[i]);				}			}		}		LogLog.debug("curSizeRollBackups starts at: " + curSizeRollBackups);		//part A ends here		//part B not yet implemented		//part C		if (staticLogFileName && rollDate) {			File old = new File(baseFileName);			if (old.exists()) {				Date last = new Date(old.lastModified());				if (!(sdf.format(last).equals(sdf.format(now)))) {					scheduledFilename = baseFileName + sdf.format(last);					LogLog.debug("Initial roll over to: " + scheduledFilename);					rollOverTime();				}			}		}		LogLog.debug("curSizeRollBackups after rollOver at: " + curSizeRollBackups);		//part C ends here	}	/**	 * Sets initial conditions including date/time roll over information, first check,	 * scheduledFilename, and calls <code>existingInit</code> to initialize	 * the current # of backups.	 */	public void activateOptions() {	    //REMOVE removed rollDate from boolean to enable Alex's change		if(datePattern != null) {			now.setTime(System.currentTimeMillis());			sdf = new SimpleDateFormat(datePattern);			int type = computeCheckPeriod();			//printPeriodicity(type);			rc.setType(type);			//next line added as this removes the name check in rollOver			nextCheck = rc.getNextCheckMillis(now);		} else {			if (rollDate)			    LogLog.error("Either DatePattern or rollingStyle options are not set for ["+			      name+"].");		}		existingInit();		super.activateOptions();		if (rollDate && fileName != null && scheduledFilename == null)			scheduledFilename = fileName + sdf.format(now);	}	/**	 Rollover the file(s) to date/time tagged file(s).	 Opens the new file (through setFile) and resets curSizeRollBackups.	*/	protected void rollOverTime() {	    curTimeRollBackups++;		//delete the old stuff here		if (staticLogFileName) {			/* Compute filename, but only if datePattern is specified */			if (datePattern == null) {				errorHandler.error("Missing DatePattern option in rollOver().");				return;			}			//is the new file name equivalent to the 'current' one			//something has gone wrong if we hit this -- we should only			//roll over if the new file will be different from the old			String dateFormat = sdf.format(now);			if (scheduledFilename.equals(fileName + dateFormat)) {				errorHandler.error("Compare " + scheduledFilename + " : " + fileName + dateFormat);				return;			}			// close current file, and rename it to datedFilename			this.closeFile();			//we may have to roll over a large number of backups here	        String from, to;			for (int i = 1; i <= curSizeRollBackups; i++) {				from = fileName + '.' + i;				to = scheduledFilename + '.' + i;				rollFile(from, to);	        }			rollFile(fileName, scheduledFilename);		}		try {			// This will also close the file. This is OK since multiple			// close operations are safe.			curSizeRollBackups = 0; //We're cleared out the old date and are ready for the new			//new scheduled name			scheduledFilename = fileName + sdf.format(now);			this.setFile(baseFileName, false);		}		catch(IOException e) {			errorHandler.error("setFile("+fileName+", false) call failed.");		}	}	/** Renames file <code>from</code> to file <code>to</code>.  It	 *  also checks for existence of target file and deletes if it does.	 */	protected static void rollFile(String from, String to) {		File target = new File(to);		if (target.exists()) {			LogLog.debug("deleting existing target file: " + target);			target.delete();		}		File file = new File(from);		file.renameTo(target);		LogLog.debug(from +" -> "+ to);	}	/** Delete's the specified file if it exists */	protected static void deleteFile(String fileName) {		File file = new File(fileName);		if (file.exists()) {		   file.delete();		}	}	/**	 Implements roll overs base on file size.	 <p>If the maximum number of size based backups is reached	 (<code>curSizeRollBackups == maxSizeRollBackups</code) then the oldest	 file is deleted -- it's index determined by the sign of countDirection.<br>	 If <code>countDirection</code> < 0, then files	 {<code>File.1</code>, ..., <code>File.curSizeRollBackups -1</code>}	 are renamed to {<code>File.2</code>, ...,	 <code>File.curSizeRollBackups</code>}.	 Moreover, <code>File</code> is	 renamed <code>File.1</code> and closed.<br>	 A new file is created to receive further log output.	 <p>If <code>maxSizeRollBackups</code> is equal to zero, then the	 <code>File</code> is truncated with no backup files created.	 <p>If <code>maxSizeRollBackups</code> < 0, then <code>File</code> is	 renamed if needed and no files are deleted.	*/	// synchronization not necessary since doAppend is alreasy synched	protected void rollOverSize() {		File file;		this.closeFile(); // keep windows happy.		LogLog.debug("rolling over count=" + ((CountingQuietWriter) qw).getCount());		LogLog.debug("maxSizeRollBackups = " + maxSizeRollBackups);		LogLog.debug("curSizeRollBackups = " + curSizeRollBackups);		LogLog.debug("countDirection = " + countDirection);		// If maxBackups <= 0, then there is no file renaming to be done.		if (maxSizeRollBackups != 0) {			if (countDirection < 0) {				// Delete the oldest file, to keep Windows happy.				if (curSizeRollBackups == maxSizeRollBackups) {				    deleteFile(fileName + '.' + maxSizeRollBackups);					curSizeRollBackups--;				}				// Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}				for (int i = curSizeRollBackups; i >= 1; i--) {					rollFile((fileName + "." + i), (fileName + '.' + (i + 1)));				}				curSizeRollBackups++;				// Rename fileName to fileName.1				rollFile(fileName, fileName + ".1");			} //REMOVE This code branching for Alexander Cerna's request			else if (countDirection == 0) {				//rollFile based on date pattern				curSizeRollBackups++;				now.setTime(System.currentTimeMillis());				scheduledFilename = fileName + sdf.format(now);				rollFile(fileName, scheduledFilename);			}			else { //countDirection > 0				if (curSizeRollBackups >= maxSizeRollBackups && maxSizeRollBackups > 0) {					//delete the first and keep counting up.					int oldestFileIndex = curSizeRollBackups - maxSizeRollBackups + 1;					deleteFile(fileName + '.' + oldestFileIndex);				}				if (staticLogFileName) {					curSizeRollBackups++;					rollFile(fileName, fileName + '.' + curSizeRollBackups);				}			}		}		try {			// This will also close the file. This is OK since multiple			// close operations are safe.			this.setFile(baseFileName, false);		}		catch(IOException e) {			LogLog.error("setFile("+fileName+", false) call failed.", e);		}	}}

⌨️ 快捷键说明

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