How do you flush a buffered log4j FileAppender?

前端 未结 7 1752
感动是毒
感动是毒 2020-12-01 09:49

In log4j, when using a FileAppender with BufferedIO=true and BufferSize=xxx properties (i.e. buffering is enabled), I want to be able to flush the log during normal shutdown

7条回答
  •  被撕碎了的回忆
    2020-12-01 10:00

    Sharing my experience with using "Andrey Kurilov"'s code example, or at least simmilar.

    What I actually wanted to achieve was to implement an asynchronous log entries with manual flush (immediateFlush = false) to ensure an idle buffers content will be flushed before the bufferSize is reached.

    The initial performance results were actually comparable with the ones achieved with the AsyncAppender - so I think it is a good alternative of it.

    The AsyncAppender is using a separate thread (and additional dependency to disruptor jar), which makes it more performant, but with the cost of more CPU and even more disk flushing(no matter with high load flushes are made on batches).

    So if you want to save disk IO operations and CPU load, but still want to ensure your buffers will be flushed asynchronously at some point, that is the way to go.

提交回复
热议问题