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就会执行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属于根据事件驱动,而不是时间调度,因此会在写入日志判断是否进行删除.
RollingFileAppender类subAppend

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

        super.subAppend(event);
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容