logback FixedWindowRollingPolicy固定窗口算法重命名文件滾動(dòng)策略
序
本文主要研究一下logback的FixedWindowRollingPolicy
RollingPolicy
ch/qos/logback/core/rolling/RollingPolicy.java
/** * A <code>RollingPolicy</code> is responsible for performing the rolling over * of the active log file. The <code>RollingPolicy</code> is also responsible * for providing the <em>active log file</em>, that is the live file where * logging output will be directed. * * @author Ceki Gülcü */ public interface RollingPolicy extends LifeCycle { /** * Rolls over log files according to implementation policy. * * <p> * This method is invoked by {@link RollingFileAppender}, usually at the behest * of its {@link TriggeringPolicy}. * * @throws RolloverFailure Thrown if the rollover operation fails for any * reason. */ void rollover() throws RolloverFailure; /** * Get the name of the active log file. * * <p> * With implementations such as {@link TimeBasedRollingPolicy}, this method * returns a new file name, where the actual output will be sent. * * <p> * On other implementations, this method might return the FileAppender's file * property. */ String getActiveFileName(); /** * The compression mode for this policy. * * @return */ CompressionMode getCompressionMode(); /** * This method allows RollingPolicy implementations to be aware of their * containing appender. * * @param appender */ void setParent(FileAppender<?> appender); }
RollingPolicy接口定義了rollover、getActiveFileName、getCompressionMode、setParent方法
RollingPolicyBase
ch/qos/logback/core/rolling/RollingPolicyBase.java
/** * Implements methods common to most, it not all, rolling policies. Currently * such methods are limited to a compression mode getter/setter. * * @author Ceki Gülcü */ public abstract class RollingPolicyBase extends ContextAwareBase implements RollingPolicy { protected CompressionMode compressionMode = CompressionMode.NONE; FileNamePattern fileNamePattern; // fileNamePatternStr is always slashified, see setter protected String fileNamePatternStr; private FileAppender<?> parent; // use to name files within zip file, i.e. the zipEntry FileNamePattern zipEntryFileNamePattern; private boolean started; /** * Given the FileNamePattern string, this method determines the compression mode * depending on last letters of the fileNamePatternStr. Patterns ending with .gz * imply GZIP compression, endings with '.zip' imply ZIP compression. Otherwise * and by default, there is no compression. * */ protected void determineCompressionMode() { if (fileNamePatternStr.endsWith(".gz")) { addInfo("Will use gz compression"); compressionMode = CompressionMode.GZ; } else if (fileNamePatternStr.endsWith(".zip")) { addInfo("Will use zip compression"); compressionMode = CompressionMode.ZIP; } else { addInfo("No compression will be used"); compressionMode = CompressionMode.NONE; } } //...... }
RollingPolicyBase定義了compressionMode、fileNamePattern、fileNamePatternStr、parent、zipEntryFileNamePattern、started;
determineCompressionMode方法會(huì)根據(jù)fileNamePatternStr的后綴來判斷,默認(rèn)支持gz、zip
FixedWindowRollingPolicy
ch/qos/logback/core/rolling/FixedWindowRollingPolicy.java
public class FixedWindowRollingPolicy extends RollingPolicyBase { static final String FNP_NOT_SET = "The \"FileNamePattern\" property must be set before using FixedWindowRollingPolicy. "; static final String PRUDENT_MODE_UNSUPPORTED = "See also " + CODES_URL + "#tbr_fnp_prudent_unsupported"; static final String SEE_PARENT_FN_NOT_SET = "Please refer to " + CODES_URL + "#fwrp_parentFileName_not_set"; int maxIndex; int minIndex; RenameUtil util = new RenameUtil(); Compressor compressor; public static final String ZIP_ENTRY_DATE_PATTERN = "yyyy-MM-dd_HHmm"; /** * It's almost always a bad idea to have a large window size, say over 20. */ private static int MAX_WINDOW_SIZE = 20; public FixedWindowRollingPolicy() { minIndex = 1; maxIndex = 7; } //...... }
FixedWindowRollingPolicy繼承了RollingPolicyBase,他定義了minIndex、maxIndex、compressor屬性
start
public void start() { util.setContext(this.context); if (fileNamePatternStr != null) { fileNamePattern = new FileNamePattern(fileNamePatternStr, this.context); determineCompressionMode(); } else { addError(FNP_NOT_SET); addError(CoreConstants.SEE_FNP_NOT_SET); throw new IllegalStateException(FNP_NOT_SET + CoreConstants.SEE_FNP_NOT_SET); } if (isParentPrudent()) { addError("Prudent mode is not supported with FixedWindowRollingPolicy."); addError(PRUDENT_MODE_UNSUPPORTED); throw new IllegalStateException("Prudent mode is not supported."); } if (getParentsRawFileProperty() == null) { addError("The File name property must be set before using this rolling policy."); addError(SEE_PARENT_FN_NOT_SET); throw new IllegalStateException("The \"File\" option must be set."); } if (maxIndex < minIndex) { addWarn("MaxIndex (" + maxIndex + ") cannot be smaller than MinIndex (" + minIndex + ")."); addWarn("Setting maxIndex to equal minIndex."); maxIndex = minIndex; } final int maxWindowSize = getMaxWindowSize(); if ((maxIndex - minIndex) > maxWindowSize) { addWarn("Large window sizes are not allowed."); maxIndex = minIndex + maxWindowSize; addWarn("MaxIndex reduced to " + maxIndex); } IntegerTokenConverter itc = fileNamePattern.getIntegerTokenConverter(); if (itc == null) { throw new IllegalStateException( "FileNamePattern [" + fileNamePattern.getPattern() + "] does not contain a valid IntegerToken"); } if (compressionMode == CompressionMode.ZIP) { String zipEntryFileNamePatternStr = transformFileNamePatternFromInt2Date(fileNamePatternStr); zipEntryFileNamePattern = new FileNamePattern(zipEntryFileNamePatternStr, context); } compressor = new Compressor(compressionMode); compressor.setContext(this.context); super.start(); }
start方法先根據(jù)fileNamePattern來創(chuàng)建FileNamePattern,然后判斷壓縮模式,然后校驗(yàn)minIndex及maxIndex,要求相差不能超過MAX_WINDOW_SIZE(默認(rèn)值為20),之后判斷如果是zip模式的則創(chuàng)建zipEntryFileNamePattern,最后根據(jù)壓縮模式創(chuàng)建compressor
rollover
public void rollover() throws RolloverFailure { // Inside this method it is guaranteed that the hereto active log file is // closed. // If maxIndex <= 0, then there is no file renaming to be done. if (maxIndex >= 0) { // Delete the oldest file, to keep Windows happy. File file = new File(fileNamePattern.convertInt(maxIndex)); if (file.exists()) { file.delete(); } // Map {(maxIndex - 1), ..., minIndex} to {maxIndex, ..., minIndex+1} for (int i = maxIndex - 1; i >= minIndex; i--) { String toRenameStr = fileNamePattern.convertInt(i); File toRename = new File(toRenameStr); // no point in trying to rename a nonexistent file if (toRename.exists()) { util.rename(toRenameStr, fileNamePattern.convertInt(i + 1)); } else { addInfo("Skipping roll-over for inexistent file " + toRenameStr); } } // move active file name to min switch (compressionMode) { case NONE: util.rename(getActiveFileName(), fileNamePattern.convertInt(minIndex)); break; case GZ: compressor.compress(getActiveFileName(), fileNamePattern.convertInt(minIndex), null); break; case ZIP: compressor.compress(getActiveFileName(), fileNamePattern.convertInt(minIndex), zipEntryFileNamePattern.convert(new Date())); break; } } }
rollover方法從maxIndex-1開始到minIndex,把這些文件名的序號(hào)加1,之后根據(jù)壓縮模式判斷,如果不壓縮則把當(dāng)前文件名重名為minIndex,若是gz壓縮則把當(dāng)前文件壓縮然后命名為minIndex,若是zip壓縮則把當(dāng)前文件壓縮然后命名為minIndex加上日期
小結(jié)
logback的FixedWindowRollingPolicy繼承了RollingPolicyBase,實(shí)現(xiàn)了RollingPolicy接口,該接口定義了rollover、getActiveFileName、getCompressionMode、setParent方法,其中FixedWindowRollingPolicy的rollover的實(shí)現(xiàn)是根據(jù)minIndex及maxIndex來的,要求maxIndex及minIndex相差不能超過20,rollover的時(shí)候從maxIndex-1開始到minIndex,把這些文件名的序號(hào)加1,然后當(dāng)前文件重命名為minIndex,其中還配合壓縮模式進(jìn)行壓縮處理。
以上就是logback FixedWindowRollingPolicy固定窗口算法重命名文件滾動(dòng)策略的詳細(xì)內(nèi)容,更多關(guān)于logback FixedWindowRollingPolicy的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring框架+jdbcTemplate實(shí)現(xiàn)增刪改查功能
這篇文章主要介紹了Spring框架+jdbcTemplate實(shí)現(xiàn)增刪改查功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09java中如何使用BufferedImage判斷圖像通道順序并轉(zhuǎn)RGB/BGR
這篇文章主要介紹了java中如何BufferedImage判斷圖像通道順序并轉(zhuǎn)RGB/BGR的相關(guān)資料,需要的朋友可以參考下2017-03-03Java中Socket設(shè)置連接超時(shí)的代碼分享
在我們?nèi)粘_B接中,如果超時(shí)時(shí)長(zhǎng)過長(zhǎng)的話,在開發(fā)時(shí)會(huì)影響測(cè)試,下面這篇文章主要給大家分享了關(guān)于Java中Socket設(shè)置連接超時(shí)的代碼,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06一篇文章掌握J(rèn)ava?Thread的類及其常見方法
Thread類用于操作線程,是所以涉及到線程操作(如并發(fā))的基礎(chǔ)。本文將通過代碼對(duì)Thread類的功能作用及其常見方法進(jìn)行分析2022-03-03Java兩個(gè)List<T> 求交集,差集,并集,去重后的并集
本文主要介紹了Java兩個(gè)List<T> 求交集,差集,并集,去重后的并集,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04在Intellij Idea中使用jstl標(biāo)簽庫(kù)的方法
這篇文章主要介紹了在Intellij Idea中使用jstl標(biāo)簽庫(kù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05AJAX中Get請(qǐng)求報(bào)錯(cuò)404的原因以及解決辦法
剛學(xué)習(xí)一門技術(shù)時(shí)總會(huì)踩一些坑,下面這篇文章主要給大家介紹了關(guān)于AJAX中Get請(qǐng)求報(bào)錯(cuò)404的原因及解決辦法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03