Logback中异步压缩任务实现

有些时候我们需要在业务主流程外做一些不影响主流程的操作,比如发生通知,开启一个异步任务,我们不关心这些额外操作的执行成功与否,但不能影响主流程。一般这种场景下,都会使用异步线程处理,下面带着源码,来看下Logback在进行日志归档的过程中如何通过异步任务实现日志压缩。希望大家举一反三,领会其设计精髓,方便以后运行在自己的代码中。

场景描述

Logback在进行日志归档过程中主要处理一下几件事:
1.文件名转换,将当前活动文件名转换成归档文件名。执行该步骤的条件是<appender>配置了<file> 属性,当前活动文件名是<file> 属性值,归档文件名是<fileNamePatternStr>的格式。
2.归档文件压缩,执行该步骤的条件是<fileNamePatternStr>属性值中以.gz/.zip等后缀结尾。
3.历史归档文件删除,执行该步骤的条件是配置了<maxHistory>。

// ch.qos.logback.core.rolling.TimeBasedRollingPolicy#rollover
//文件归档操作
public void rollover() throws RolloverFailure {
    //归档文件名全路径,如logs/test.log
    String elapsedPeriodsFileName = timeBasedFileNamingAndTriggeringPolicy.getElapsedPeriodsFileName();
    //归档文件名称,如test.log
    String elapsedPeriodStem = FileFilterUtil.afterLastSlash(elapsedPeriodsFileName);
    //判断是否配置压缩
    if (compressionMode == CompressionMode.NONE) {
        //判断<appender>中是否配置了<file>属性
        if (getParentsRawFileProperty() != null) {
            //将当前活动文件名转换为归档文件名
            renameUtil.rename(getParentsRawFileProperty(), elapsedPeriodsFileName);
         } 
     } else {
        if (getParentsRawFileProperty() == null) {
            //直接将归档文件压缩
            compressionFuture = compressor.asyncCompress(elapsedPeriodsFileName, elapsedPeriodsFileName, elapsedPeriodStem);
        } else {
            //先根据<file>创建一个临时文件和一个新的<file>文件,再将临时文件名转换为归档文件名,然后将归档文件压缩
            compressionFuture = renameRawAndAsyncCompress(elapsedPeriodsFileName, elapsedPeriodStem);
        }
    }
    //判断是否配置<maxHistory>
    if (archiveRemover != null) {
        Date now = new Date(timeBasedFileNamingAndTriggeringPolicy.getCurrentTime());
        this.cleanUpFuture = archiveRemover.cleanAsynchronously(now);
     }
}

这里面归档文件的压缩和历史文件的删除都使用的是异步任务。压缩操作使用Compressor类处理,删除操作使用ArchiveRemover类处理

//ch.qos.logback.core.rolling.helper.Compressor#asyncCompress
public Future<?> asyncCompress(String nameOfFile2Compress, String nameOfCompressedFile, String innerEntryName) throws RolloverFailure {
    //创建异步任务
    CompressionRunnable runnable = new CompressionRunnable(nameOfFile2Compress, nameOfCompressedFile, innerEntryName);
    //获取任务线程池
    ExecutorService executorService = context.getScheduledExecutorService();
    //执行异步任务
    Future<?> future = executorService.submit(runnable);
    return future;
 }

//ch.qos.logback.core.rolling.helper.Compressor.CompressionRunnable
class CompressionRunnable implements Runnable {
    final String nameOfFile2Compress;
    final String nameOfCompressedFile;
    final String innerEntryName;

    public CompressionRunnable(String nameOfFile2Compress, String nameOfCompressedFile, String innerEntryName) {
      this.nameOfFile2Compress = nameOfFile2Compress;
      this.nameOfCompressedFile = nameOfCompressedFile;
      this.innerEntryName = innerEntryName;
     }

    public void run() {
        //调用Compressor实例中的compress方法压缩文件
        Compressor.this.compress(nameOfFile2Compress, nameOfCompressedFile, innerEntryName);
    }
 }

CompressionRunnable 实际上是没有返回值的,但是asyncCompress方法还是返回了任务执行结果,那这个空的结果有什么用呢?

  //ch.qos.logback.core.rolling.TimeBasedRollingPolicy#stop
@Override
public void stop() {
   if (!isStarted())
      return;
    waitForAsynchronousJobToStop(compressionFuture,"compression");
    waitForAsynchronousJobToStop(cleanUpFuture, "clean-up");
    super.stop();
   }

private void waitForAsynchronousJobToStop(Future<?> aFuture, String jobDescription) {
    if (aFuture != null) {
       try {
       // 等待指定时间,如果任务还未执行完,就抛出异常
         aFuture.get(CoreConstants.SECONDS_TO_WAIT_FOR_COMPRESSION_JOBS, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            addError("Timeout while waiting for " + jobDescription + " job to finish", e);
        } catch (Exception e) {
           addError("Unexpected exception while waiting for " + jobDescription + " job to finish", e);
       }
    }
}

在日志系统停止时,如果压缩操作还在进行,等待一定时间,超过该时间抛出异常。

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

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,845评论 18 139
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,478评论 9 467
  • logback详解 说明:下面内容从网上搜集整理而来 与log4j对比 更快的执行速度: 基于我们先前在log4j...
    亿万年星空阅读 3,078评论 1 1
  • 一、logback的介绍Logback是由log4j创始人设计的另一个开源日志组件,官方网站: http://lo...
    Running小琦阅读 797评论 0 8
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,926评论 6 342