最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

logback FixedWindowRollingPolicy固定窗口算法重命名文件滾動策略

 更新時間:2023年11月09日 09:17:22   作者:codecraft  
這篇文章主要介紹了FixedWindowRollingPolicy根據(jù)logback 固定窗口算法重命名文件滾動策略源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

本文主要研究一下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&uuml;lc&uuml;
 */
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&uuml;lc&uuml;
 */
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方法會根據(jù)fileNamePatternStr的后綴來判斷,默認支持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,然后判斷壓縮模式,然后校驗minIndex及maxIndex,要求相差不能超過MAX_WINDOW_SIZE(默認值為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,把這些文件名的序號加1,之后根據(jù)壓縮模式判斷,如果不壓縮則把當(dāng)前文件名重名為minIndex,若是gz壓縮則把當(dāng)前文件壓縮然后命名為minIndex,若是zip壓縮則把當(dāng)前文件壓縮然后命名為minIndex加上日期

小結(jié)

logback的FixedWindowRollingPolicy繼承了RollingPolicyBase,實現(xiàn)了RollingPolicy接口,該接口定義了rollover、getActiveFileName、getCompressionMode、setParent方法,其中FixedWindowRollingPolicy的rollover的實現(xiàn)是根據(jù)minIndex及maxIndex來的,要求maxIndex及minIndex相差不能超過20,rollover的時候從maxIndex-1開始到minIndex,把這些文件名的序號加1,然后當(dāng)前文件重命名為minIndex,其中還配合壓縮模式進行壓縮處理。

以上就是logback FixedWindowRollingPolicy固定窗口算法重命名文件滾動策略的詳細內(nèi)容,更多關(guān)于logback FixedWindowRollingPolicy的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • spring boot測試打包部署的方法

    spring boot測試打包部署的方法

    spring boot項目如何測試,如何部署,在生產(chǎn)中有什么好的部署方案嗎?這篇文章就來介紹一下spring boot 如何開發(fā)、調(diào)試、打包到最后的投產(chǎn)上線,感興趣的朋友一起看看吧
    2018-01-01
  • MyEclipse去除網(wǎng)上復(fù)制下來的代碼帶有的行號(正則去除行號)

    MyEclipse去除網(wǎng)上復(fù)制下來的代碼帶有的行號(正則去除行號)

    這篇文章主要介紹了MyEclipse去除網(wǎng)上復(fù)制下來的代碼帶有的行號(正則去除行號)的相關(guān)資料,需要的朋友可以參考下
    2017-10-10
  • java Disruptor構(gòu)建高性能內(nèi)存隊列使用詳解

    java Disruptor構(gòu)建高性能內(nèi)存隊列使用詳解

    這篇文章主要為大家介紹了java Disruptor構(gòu)建高性能內(nèi)存隊列使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • springMVC RequestMapping注解的實現(xiàn)過程

    springMVC RequestMapping注解的實現(xiàn)過程

    springMVC是一個實現(xiàn)了mvc架構(gòu)模式的web框架,底層基于servlet實現(xiàn),本文給大家介紹springMVC RequestMapping注解的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧
    2026-02-02
  • Java中IO流文件讀取、寫入和復(fù)制的實例

    Java中IO流文件讀取、寫入和復(fù)制的實例

    下面小編就為大家?guī)硪黄狫ava中IO流文件讀取、寫入和復(fù)制的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 關(guān)于RedisTemplate之opsForValue的使用說明

    關(guān)于RedisTemplate之opsForValue的使用說明

    這篇文章主要介紹了關(guān)于RedisTemplate之opsForValue的使用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • SpringBoot中@Transiactional注解沒有效果的解決

    SpringBoot中@Transiactional注解沒有效果的解決

    這篇文章主要介紹了SpringBoot中@Transiactional注解沒有效果的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 使用Java動態(tài)創(chuàng)建Flowable會簽?zāi)P偷氖纠a

    使用Java動態(tài)創(chuàng)建Flowable會簽?zāi)P偷氖纠a

    動態(tài)創(chuàng)建流程模型,尤其是會簽(Parallel Gateway)模型,是提升系統(tǒng)靈活性和響應(yīng)速度的關(guān)鍵技術(shù)之一,本文將通過Java編程語言,深入探討如何在運行時動態(tài)地創(chuàng)建包含會簽環(huán)節(jié)的Flowable流程模型,需要的朋友可以參考下
    2024-05-05
  • Java前后端時間格式的轉(zhuǎn)化方式

    Java前后端時間格式的轉(zhuǎn)化方式

    這篇文章主要介紹了Java前后端時間格式的轉(zhuǎn)化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 利用java和sqlserver建立簡易圖書管理系統(tǒng)的完整步驟

    利用java和sqlserver建立簡易圖書管理系統(tǒng)的完整步驟

    圖書館管理系統(tǒng)是圖書館管理工作中不可缺少的部分,它對于圖書館的管理者和使用者都非常重要,下面這篇文章主要給大家介紹了關(guān)于利用java和sqlserver建立簡易圖書管理系統(tǒng)的完整步驟,需要的朋友可以參考下
    2022-06-06

最新評論

原阳县| 新巴尔虎右旗| 元江| 射洪县| 广宗县| 高要市| 灵璧县| 成武县| 黔东| 图片| 车致| 抚顺县| 营口市| 中山市| 宜宾县| 昭觉县| 尼勒克县| 济南市| 临沂市| 香格里拉县| 屏南县| 溧水县| 壶关县| 垣曲县| 志丹县| 武陟县| 宁夏| 四平市| 富川| 福海县| 巴塘县| 赣榆县| 正安县| 大庆市| 桂平市| 东辽县| 德阳市| 互助| 柘城县| 永平县| 深水埗区|