0

I've been looking at optimizing my Log4J configuration to improve the throughput and reduce latency, and I'm trying to determine the best settings. I'm dealing with a system where the code must be bug-free, reliable, and system performance are the top priority in order from most important to least important.

It seems pretty clear to me that Asynchronous loggers is the clear winner in terms of best performance, and that makes complete sense. I don't understand the tradeoffs of Random Access File Appenders vs. Buffered File Appenders. I looked at the log4j website here but I didn't see any real downside to using Random Access File Appenders.

Could somebody please explain the differences, and explain when each should be used?

4

2 回答 2

2

兴趣点:

  • RandomAccessFileAppenders 总是被缓冲的。从 beta-9 开始,缓冲区大小为 256 * 1024 字节且不可配置。这将在下一个版本中变得可配置。
  • FileAppenders 可以配置bufferedIO缓存或不缓存的属性。从 beta-9 开始,缓冲区大小为 8 * 1024 字节且不可配置。这将在下一个版本中变得可配置。

  • 在底层,FileAppenders 使用 java.io.FileOutputStream,它可以包装在 BufferedOutputStream 中。RandomAccessFileAppenders 写入 ByteBuffer,当缓冲区已满或被flush()调用时,该 ByteBuffer 将附加到 RandomAccessFile 的末尾。应该没有太大区别(除了性能),但是 FileAppenders 有更长的跟踪记录,并且可能仍然存在尚未发现的较新的 RandomAccessFileAppenders 问题。对于这两种类型的 appender,翻转似乎都可以正常工作,但可能存在团队还不知道的极端情况。(不过,Log4J2 正在积极开发中,任何问题都会迅速解决。)

  • RandomAccessFileAppenders 和 FileAppenders 都可以配置为将immediateFlush每个日志事件刷新到磁盘的属性。我建议您immediateFlush在使用 AsyncAppenders 或 AsyncLoggers 时关闭,这样您就可以利用它们提供的良好批处理行为:异步附加程序和记录器可以在记录多个事件后刷新,并且当队列再次为空时也肯定会刷新一次,这非常有效并且同时确保所有日志事件始终保存到磁盘。

于 2013-10-16T04:54:59.123 回答
1

随机访问直接写入文件,没有缓冲。这比缓冲写入更慢但更可靠,因为在发生崩溃时缓冲写入可能会丢失一些未刷新的写入。你需要决定哪个对你更重要。从您的三个列表中不清楚。

编辑人们会期望一个名为的类RandomAccessAppender将使用随机访问,这意味着没有缓冲,但显然它没有!

于 2013-10-14T22:58:11.280 回答