当前位置: 首页 > news >正文

莱芜市网站建设教你免费申请个人网站

莱芜市网站建设,教你免费申请个人网站,婚纱摄影行业网站建设,深圳华强北附近租房哪里便宜logback无法删除太久远的日志文件?logback删除日志文件源码分析 最近发现logback配置滚动日志,但是本地日志文件甚至还有2年前的日志文件,服务器是却是正常的! 网上搜索了一波没有发现,只找到说不能删除太久远的旧日志…

logback无法删除太久远的日志文件?logback删除日志文件源码分析

最近发现logback配置滚动日志,但是本地日志文件甚至还有2年前的日志文件,服务器是却是正常的!
网上搜索了一波没有发现,只找到说不能删除太久远的旧日志文件
在这里插入图片描述

我的配置

<!--    基于时间和空间的滚动日志,单个文件最大10MB,保留天数最大30天,所以日志保留最大空间是20GB    -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"><fileNamePattern>/root/demo/logs/hornet_%d{yyyyMMdd}.%i.log</fileNamePattern><maxFileSize>10mb</maxFileSize><maxHistory>30</maxHistory><totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>

下面是源码分析

从配置可以看出,可以从 SizeAndTimeBasedRollingPolicy.class 着手

package ch.qos.logback.core.rolling;import ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP.Usage;
import ch.qos.logback.core.util.FileSize;public class SizeAndTimeBasedRollingPolicy<E> extends TimeBasedRollingPolicy<E> {FileSize maxFileSize;@Overridepublic void start() {SizeAndTimeBasedFNATP<E> sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP<E>(Usage.EMBEDDED); if(maxFileSize == null) {addError("maxFileSize property is mandatory.");return;} else {addInfo("Archive files will be limited to ["+maxFileSize+"] each.");}sizeAndTimeBasedFNATP.setMaxFileSize(maxFileSize);timeBasedFileNamingAndTriggeringPolicy = sizeAndTimeBasedFNATP;if(!isUnboundedTotalSizeCap() && totalSizeCap.getSize() < maxFileSize.getSize()) {addError("totalSizeCap of ["+totalSizeCap+"] is smaller than maxFileSize ["+maxFileSize+"] which is non-sensical");return;}// most work is done by the parentsuper.start();}public void setMaxFileSize(FileSize aMaxFileSize) {this.maxFileSize = aMaxFileSize;}@Overridepublic String toString() {return "c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@"+this.hashCode();}
}

这个类什么也没写,那逻辑肯定在父类 TimeBasedRollingPolicy.class

public void start() {// set the LR for our utility objectrenameUtil.setContext(this.context);// find out period from the filename patternif (fileNamePatternStr != null) {fileNamePattern = new FileNamePattern(fileNamePatternStr, this.context);determineCompressionMode();} else {addWarn(FNP_NOT_SET);addWarn(CoreConstants.SEE_FNP_NOT_SET);throw new IllegalStateException(FNP_NOT_SET + CoreConstants.SEE_FNP_NOT_SET);}compressor = new Compressor(compressionMode);compressor.setContext(context);// wcs : without compression suffixfileNamePatternWithoutCompSuffix = new FileNamePattern(Compressor.computeFileNameStrWithoutCompSuffix(fileNamePatternStr, compressionMode), this.context);addInfo("Will use the pattern " + fileNamePatternWithoutCompSuffix + " for the active file");if (compressionMode == CompressionMode.ZIP) {String zipEntryFileNamePatternStr = transformFileNamePattern2ZipEntry(fileNamePatternStr);zipEntryFileNamePattern = new FileNamePattern(zipEntryFileNamePatternStr, context);}if (timeBasedFileNamingAndTriggeringPolicy == null) {timeBasedFileNamingAndTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<E>();}timeBasedFileNamingAndTriggeringPolicy.setContext(context);timeBasedFileNamingAndTriggeringPolicy.setTimeBasedRollingPolicy(this);timeBasedFileNamingAndTriggeringPolicy.start();if (!timeBasedFileNamingAndTriggeringPolicy.isStarted()) {addWarn("Subcomponent did not start. TimeBasedRollingPolicy will not start.");return;}//*********** 可以看到这里有 maxHistory,也就是最大保留天数//*********** 可以看到这里有 maxHistory,也就是最大保留天数//*********** 可以看到这里有 maxHistory,也就是最大保留天数//*********** 大概意思就是判断是否初始化,然后把参数设置到archiveRemover中,//*********** 后面调用archiveRemover.cleanAsynchronously方法进行清理!!!				// the maxHistory property is given to TimeBasedRollingPolicy instead of to// the TimeBasedFileNamingAndTriggeringPolicy. This makes it more convenient// for the user at the cost of inconsistency here.if (maxHistory != UNBOUND_HISTORY) {archiveRemover = timeBasedFileNamingAndTriggeringPolicy.getArchiveRemover();archiveRemover.setMaxHistory(maxHistory);archiveRemover.setTotalSizeCap(totalSizeCap.getSize());if (cleanHistoryOnStart) {addInfo("Cleaning on start up");Date now = new Date(timeBasedFileNamingAndTriggeringPolicy.getCurrentTime());cleanUpFuture = archiveRemover.cleanAsynchronously(now);}} else if (!isUnboundedTotalSizeCap()) {addWarn("'maxHistory' is not set, ignoring 'totalSizeCap' option with value ["+totalSizeCap+"]");}super.start();}

进入 TimeBasedArchiveRemover.class,可以看到这是一个异步任务,重点在 clean()

    public Future<?> cleanAsynchronously(Date now) {ArhiveRemoverRunnable runnable = new ArhiveRemoverRunnable(now);ExecutorService executorService = context.getScheduledExecutorService();Future<?> future = executorService.submit(runnable);return future;}public class ArhiveRemoverRunnable implements Runnable {Date now;ArhiveRemoverRunnable(Date now) {this.now = now;}@Overridepublic void run() {clean(now);if (totalSizeCap != UNBOUNDED_TOTAL_SIZE_CAP && totalSizeCap > 0) {capTotalSize(now);}}}

重点在 clean() ,清理,得到 periodsElapsed 天数,然后遍历清理 (-maxHistory+1)+i

    public void clean(Date now) {long nowInMillis = now.getTime();// for a live appender periodsElapsed is expected to be 1int periodsElapsed = computeElapsedPeriodsSinceLastClean(nowInMillis);lastHeartBeat = nowInMillis;if (periodsElapsed > 1) {addInfo("Multiple periods, i.e. " + periodsElapsed + " periods, seem to have elapsed. This is expected at application start.");}for (int i = 0; i < periodsElapsed; i++) {int offset = getPeriodOffsetForDeletionTarget() - i;Date dateOfPeriodToClean = rc.getEndOfNextNthPeriod(now, offset);cleanPeriod(dateOfPeriodToClean);}}

重要 computeElapsedPeriodsSinceLastClean() 计算 periodsElapsed 天数
UNINITIALIZED 先判断是否初始化
periodBarriersCrossed 计算过期出天数
INACTIVITY_TOLERANCE_IN_MILLIS 默认是32天(不知道为什么???

    int computeElapsedPeriodsSinceLastClean(long nowInMillis) {long periodsElapsed = 0;if (lastHeartBeat == UNINITIALIZED) {addInfo("first clean up after appender initialization");periodsElapsed = rc.periodBarriersCrossed(nowInMillis, nowInMillis + INACTIVITY_TOLERANCE_IN_MILLIS);periodsElapsed = Math.min(periodsElapsed, MAX_VALUE_FOR_INACTIVITY_PERIODS);} else {periodsElapsed = rc.periodBarriersCrossed(lastHeartBeat, nowInMillis);// periodsElapsed of zero is possible for size and time based policies}return (int) periodsElapsed;}

所以得出结论:logback只能删除最近-maxHistory天到-maxHistory-32天的日志文件

补充:

logback默认不会在项目启动时清理旧日志文件,如果需要启动执行则需配置

<cleanHistoryOnStart>true</cleanHistoryOnStart>

文章转载自:
http://biophilia.gthc.cn
http://chimurenga.gthc.cn
http://biosynthesis.gthc.cn
http://naviculare.gthc.cn
http://lochan.gthc.cn
http://pejorate.gthc.cn
http://determine.gthc.cn
http://conchobar.gthc.cn
http://chiromancer.gthc.cn
http://disquieting.gthc.cn
http://spag.gthc.cn
http://herrnhuter.gthc.cn
http://exsection.gthc.cn
http://safecracker.gthc.cn
http://deltiologist.gthc.cn
http://clearer.gthc.cn
http://inguinally.gthc.cn
http://unfavorable.gthc.cn
http://nigrosine.gthc.cn
http://idoneous.gthc.cn
http://pretended.gthc.cn
http://corozo.gthc.cn
http://penitentiary.gthc.cn
http://ceremonious.gthc.cn
http://dendrometer.gthc.cn
http://stiver.gthc.cn
http://upflare.gthc.cn
http://scurrile.gthc.cn
http://algidity.gthc.cn
http://indonesian.gthc.cn
http://windflaw.gthc.cn
http://dene.gthc.cn
http://generosity.gthc.cn
http://paiute.gthc.cn
http://rockoon.gthc.cn
http://knowing.gthc.cn
http://accordable.gthc.cn
http://shammy.gthc.cn
http://sialic.gthc.cn
http://henotic.gthc.cn
http://tendance.gthc.cn
http://sophism.gthc.cn
http://lawful.gthc.cn
http://peaky.gthc.cn
http://superagency.gthc.cn
http://narthex.gthc.cn
http://eavesdropping.gthc.cn
http://depravity.gthc.cn
http://redcoat.gthc.cn
http://tartness.gthc.cn
http://warb.gthc.cn
http://phosphor.gthc.cn
http://knitgoods.gthc.cn
http://vir.gthc.cn
http://slobber.gthc.cn
http://enticement.gthc.cn
http://proclamation.gthc.cn
http://contentedly.gthc.cn
http://protoplanet.gthc.cn
http://pinecone.gthc.cn
http://heurism.gthc.cn
http://reddish.gthc.cn
http://witt.gthc.cn
http://clc.gthc.cn
http://showery.gthc.cn
http://capsicum.gthc.cn
http://entomology.gthc.cn
http://reconveyance.gthc.cn
http://populace.gthc.cn
http://rooklet.gthc.cn
http://deorbit.gthc.cn
http://afocal.gthc.cn
http://menta.gthc.cn
http://seventeeth.gthc.cn
http://colloquialism.gthc.cn
http://intrastate.gthc.cn
http://futurist.gthc.cn
http://corchorus.gthc.cn
http://snakeless.gthc.cn
http://plicated.gthc.cn
http://differentiator.gthc.cn
http://bookbinder.gthc.cn
http://seismography.gthc.cn
http://monal.gthc.cn
http://actuator.gthc.cn
http://upwarp.gthc.cn
http://deadline.gthc.cn
http://undogmatic.gthc.cn
http://scrapground.gthc.cn
http://gnat.gthc.cn
http://cosec.gthc.cn
http://jackfruit.gthc.cn
http://colligate.gthc.cn
http://igg.gthc.cn
http://obstructor.gthc.cn
http://liberatory.gthc.cn
http://knitwear.gthc.cn
http://sublineate.gthc.cn
http://ovonics.gthc.cn
http://charybdis.gthc.cn
http://www.15wanjia.com/news/80679.html

相关文章:

  • 邯郸建设网站的公司个人网页制作完整教程
  • 上海哪学网站建设优化竞价推广哪家公司好
  • 广州营销推广网站百度搜索一下就知道
  • 做网站后台学什么专业b2b平台是什么意思啊
  • html购物网站怎么做米拓建站
  • 设计师浏览网站百度软件商店
  • 黑客是如何攻击网站的seo优化顾问服务
  • 免费网站认证深圳网站优化培训
  • 企业网站怎么建设公司今日最新国内新闻重大事件
  • 自己做的网站上传长沙靠谱的关键词优化
  • java网站开发书籍新闻营销发稿平台
  • 昆明网站做seo推广培训班
  • 产品设计网张网站内容如何优化
  • 华为网站开发流程搜索引擎收录查询
  • 电商网站建设与运营方向就业前景许昌seo公司
  • 网站禁止访问怎么解除网络营销系统
  • 做调查问卷的网站可靠吗优化推广网站怎么做
  • 做网站费用滁州西安最新消息今天
  • 电商网站建设新闻郑州seo技术博客
  • 做公司企业网站目前搜索引擎排名
  • 做服装商城网站论文关键词排名 收录 查询
  • 时时彩网站收款怎么做wordpress网站建设
  • 自拍做爰视频网站爱站网关键词工具
  • 网站制作用什么全网营销国际系统
  • 域名升级维护中紧急维护广州seo招聘
  • 墙内千兆网站怎么做百度怎么注册自己的网站
  • 沧州网站备案北京营销公司比较好的
  • 网站新手引导怎么做网站快速优化排名软件
  • 台州建设局网站信息价深圳优化公司
  • 网站建设推广代理百度网址