`
k1280000
  • 浏览: 195306 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

阅读更多

 

从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.

 

 

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

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论
1 楼 tcgdy0201 2015-12-19  
挖!博主好棒!博主好腻害!

相关推荐

Global site tag (gtag.js) - Google Analytics