logback日志刪除源代碼入口

最近由于logback配置刪除不生效,查看了相關源碼,此處記錄下入口:
1.啟動入口,啟動入口取決于你logback配置文件中配置的rollingPolicy,此處我配置的是SizeAndTimeBasedRollingPolicy,其中start方法:

    public void start() {
        SizeAndTimeBasedFNATP<E> sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP(Usage.EMBEDDED);
        if (this.maxFileSize == null) {
            this.addError("maxFileSize property is mandatory.");
        } else {
            this.addInfo("Archive files will be limited to [" + this.maxFileSize + "] each.");
            sizeAndTimeBasedFNATP.setMaxFileSize(this.maxFileSize);
            this.timeBasedFileNamingAndTriggeringPolicy = sizeAndTimeBasedFNATP;
            if (!this.isUnboundedTotalSizeCap() && this.totalSizeCap.getSize() < this.maxFileSize.getSize()) {
                this.addError("totalSizeCap of [" + this.totalSizeCap + "] is smaller than maxFileSize [" + this.maxFileSize + "] which is non-sensical");
            } else {
                super.start(); //入口
            }
        }
    }

查看super來到TimeBasedRollingPolicy可以看到當配置了cleanHistoryOnStart為true就會執(zhí)行cleanAsynchronously

 public void start() {
        this.renameUtil.setContext(this.context);
        if (this.fileNamePatternStr != null) {
            this.fileNamePattern = new FileNamePattern(this.fileNamePatternStr, this.context);
            this.determineCompressionMode();
            this.compressor = new Compressor(this.compressionMode);
            this.compressor.setContext(this.context);
            this.fileNamePatternWithoutCompSuffix = new FileNamePattern(Compressor.computeFileNameStrWithoutCompSuffix(this.fileNamePatternStr, this.compressionMode), this.context);
            this.addInfo("Will use the pattern " + this.fileNamePatternWithoutCompSuffix + " for the active file");
            if (this.compressionMode == CompressionMode.ZIP) {
                String zipEntryFileNamePatternStr = this.transformFileNamePattern2ZipEntry(this.fileNamePatternStr);
                this.zipEntryFileNamePattern = new FileNamePattern(zipEntryFileNamePatternStr, this.context);
            }

            if (this.timeBasedFileNamingAndTriggeringPolicy == null) {
                this.timeBasedFileNamingAndTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy();
            }

            this.timeBasedFileNamingAndTriggeringPolicy.setContext(this.context);
            this.timeBasedFileNamingAndTriggeringPolicy.setTimeBasedRollingPolicy(this);
            this.timeBasedFileNamingAndTriggeringPolicy.start();
            if (!this.timeBasedFileNamingAndTriggeringPolicy.isStarted()) {
                this.addWarn("Subcomponent did not start. TimeBasedRollingPolicy will not start.");
            } else {
                if (this.maxHistory != 0) {
                    this.archiveRemover = this.timeBasedFileNamingAndTriggeringPolicy.getArchiveRemover();
                    this.archiveRemover.setMaxHistory(this.maxHistory);
                    this.archiveRemover.setTotalSizeCap(this.totalSizeCap.getSize());
                    if (this.cleanHistoryOnStart) {
                        this.addInfo("Cleaning on start up");
                        Date now = new Date(this.timeBasedFileNamingAndTriggeringPolicy.getCurrentTime());
                        this.cleanUpFuture = this.archiveRemover.cleanAsynchronously(now); //此處便是入口
                    }
                } else if (!this.isUnboundedTotalSizeCap()) {
                    this.addWarn("'maxHistory' is not set, ignoring 'totalSizeCap' option with value [" + this.totalSizeCap + "]");
                }

                super.start();
            }
        } else {
            this.addWarn("The FileNamePattern option must be set before using TimeBasedRollingPolicy. ");
            this.addWarn("See also http://logback.qos.ch/codes.html#tbr_fnp_not_set");
            throw new IllegalStateException("The FileNamePattern option must be set before using TimeBasedRollingPolicy. See also http://logback.qos.ch/codes.html#tbr_fnp_not_set");
        }
    }

2.日志追加刪除入口,由于logback屬于根據(jù)事件驅動,而不是時間調(diào)度,因此會在寫入日志判斷是否進行刪除.
RollingFileAppender類subAppend

    protected void subAppend(E event) {
        synchronized(this.triggeringPolicy) {
            if (this.triggeringPolicy.isTriggeringEvent(this.currentlyActiveFile, event)) {
                this.rollover(); //刪除入口
            }
        }

        super.subAppend(event);
    }
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容