logback.xml immediate=false 到底缓存空间是多大

本文探讨了Logback中immediateFlush属性的作用及其对日志记录吞吐量的影响。当该属性设为false时,虽然能显著提高性能,但可能导致未及时写入磁盘的日志在应用程序退出时丢失。通过深入源码分析,发现缓冲区大小为8KB,有助于理解日志丢失的风险。

 

从logback官方网站上来,immediateFlush设置成false以后有5 quintuple倍吞吐量的提升,但是,会有部分缓存的日志不会输出到日志文件里,如果这时,appender遇到错误会导致缓存的部分丢失,但是,丢失到底是多少呢?

官方没有说明了,那么只有深挖到logback里面去看一下到底是多少了,干起来!

 

Immediate flushing of the output stream ensures that logging events are immediately written to disk and will not be lost in case your application exits without properly closing appenders. On the other hand, setting this property to 'false' is likely to quintuple (your mileage may vary) logging throughput. As mentioned previously, if immediateFlush is set to 'false' and if appenders are not closed properly when your application exits, then logging events not yet written to disk may be lost.

Below is a sample configuration for a FileAppender containing a PatternLayoutEncoder with its immediateFlush property set to 'false'.

<appender name="FILE" class="ch.qos.logback.core.FileAppender"> 
  <file>foo.log</file>
  <encoder>
    <pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern>
    <!-- this quadruples logging throughput -->
    <immediateFlush>false</immediateFlush>
  </encoder> 
</appender>

 

开始找!!!!!! 

 找到设置的地方 

       ch.qos.logback.core.rolling.RollingFileAppender

 

找到父类里面的

ch.qos.logback.core.OutputStreamAppender#writeOut

这个方法里面我们可以看到用

 

protected void writeOut(E event) throws IOException {
    this.encoder.doEncode(event);
  }
 用encode来doEncode事件的。

 

 

 

往往下走到

ch.qos.logback.core.encoder.LayoutWrappingEncoder#doEncode

 

public void doEncode(E event) throws IOException {
    String txt = layout.doLayout(event);
    outputStream.write(convertToBytes(txt));
    if (immediateFlush)
      outputStream.flush();
  }
 

 

ok,看到outputStream.flush,但是是immediateFlush=true的时候flush的,那么问题来了,

immediateFlush=false是怎么样的呢???

 

思考一下!!!

找到这个OutputStream具体实现类

找到encoder里面怎么接收到这个outputStream的

ch.qos.logback.core.encoder.LayoutWrappingEncoder#init

那么谁会调用encoder的init方法呢,想一下,应该是appender,去看一看

ch.qos.logback.core.OutputStreamAppender#encoderInit

ch.qos.logback.core.OutputStreamAppender#setOutputStream

ch.qos.logback.core.FileAppender#openFile

 

public void openFile(String file_name) throws IOException {
    lock.lock();
    try {
      File file = new File(file_name);
      if (FileUtil.isParentDirectoryCreationRequired(file)) {
        boolean result = FileUtil.createMissingParentDirectories(file);
        if (!result) {
          addError("Failed to create parent directories for ["
              + file.getAbsolutePath() + "]");
        }
      }

      ResilientFileOutputStream resilientFos = new ResilientFileOutputStream(
          file, append);
      resilientFos.setContext(context);
      setOutputStream(resilientFos);
    } finally {
      lock.unlock();
    }
  }
 

 

看到了ResilientFileOutputStream这个包装类!!! happy一下,快到了

 

public ResilientFileOutputStream(File file, boolean append)
      throws FileNotFoundException {
    this.file = file;
    fos = new FileOutputStream(file, append);
    this.os = new BufferedOutputStream(fos);
    this.presumedClean = true;
  }
 

 

可以看到os实质是一个 BufferedOutputStream ,OK,这就对了

 

/**
     * Creates a new buffered output stream to write data to the
     * specified underlying output stream.
     *
     * @param   out   the underlying output stream.
     */
    public BufferedOutputStream(OutputStream out) {
        this(out, 8192);
    }
 如果是这样的话,那么logback的outputStream是BufferedOutputStream,那么它的缓冲是8192也就是8K.

 

 

整个查找过程蛮有意思的,找时间写个代码去验证一下!!大笑

 

 

 

 

 

 

 

 

 

 

 

logback.xmllogback-test.xmlLogback 框架中用于配置日志输出的文件,但它们的用途和应用场景存在明显差异。 ### logback.xml 的作用 logback.xmlLogback 的默认配置文件,通常用于生产环境或主运行环境的日志配置。它会在应用程序启动时加载,并定义了日志的输出格式、输出位置、日志级别等信息。如果 logback.xml 文件存在,则 Logback 会使用该文件进行初始化;如果没有找到 logback.xmllogback-test.xmlLogback 会调用 BasicConfigurator 创建一个最小化配置,默认将日志输出到控制台,日志级别为 DEBUG,输出格式也由系统预设[^1]。 此外,logback.xml 的加载顺序在 application.properties 之后,因此它可以读取 application.properties 中定义的变量,例如日志路径或其他配置参数[^2]。 ### logback-test.xml 的作用 logback-test.xml 主要用于测试环境,通常是单元测试或者集成测试场景。当项目中有 logback-test.xml 文件时,Logback 会优先使用该文件进行配置。这种设计使得开发人员可以在测试阶段使用不同的日志配置,例如更详细的日志级别(如 TRACE)或不同的日志输出目标,以方便调试和验证日志行为是否符合预期。 ### 加载优先级与区别总结 Logback 在启动时会按照以下顺序查找配置文件: 1. **logback-test.xml**:优先用于测试环境。 2. **logback.xml**:用于生产环境。 3. **BasicConfigurator 提供的默认配置**:当上述两个文件都不存在时,Logback 会采用默认的最小化配置。 由于 logback.xmllogback-test.xml 的加载优先级不同,因此它们适用于不同的环境。logback-test.xml 更适合于测试阶段,而 logback.xml 则用于实际部署的应用程序。 ### 示例:logback.xml 配置结构 ```xml <configuration scan="true" scanPeriod="60 seconds" debug="true"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="debug"> <appender-ref ref="STDOUT" /> </root> </configuration> ``` ### 示例:logback-test.xml 配置结构 ```xml <configuration> <appender name="TEST-STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>TEST: %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="trace"> <appender-ref ref="TEST-STDOUT" /> </root> </configuration> ``` 通过以上示例可以看出,logback-test.xml 可能会配置更详细的日志级别(如 TRACE),并且日志输出格式也可能与 logback.xml 不同,以便在测试过程中捕获更调试信息。 ---
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值