简体   繁体   English

何时在log4j配置中使用随机访问文件附加器与缓冲文件附加器

[英]When to use Random Access File Appenders vs. Buffered File Appenders in log4j configuration

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. 我一直在寻找优化我的Log4J配置来提高吞吐量和减少延迟,我正在尝试确定最佳设置。 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. 我在这里查看了log4j网站但我没有看到使用随机访问文件附加程序的任何真正的缺点。

Could somebody please explain the differences, and explain when each should be used? 有人可以解释这些差异,并解释每个应该使用的时间吗?

Points of interest: 兴趣点:

  • RandomAccessFileAppenders are always buffered. RandomAccessFileAppenders总是被缓冲。 As of beta-9, the buffer size is 256 * 1024 bytes and not configurable. 从beta-9开始,缓冲区大小为256 * 1024字节,不可配置。 This will become configurable in the next release. 这将在下一版本中配置。
  • FileAppenders can be configured with the bufferedIO attribute to be buffered or not. 可以使用bufferedIO属性配置FileAppenders以进行缓冲。 As of beta-9, the buffer size is 8 * 1024 bytes and not configurable. 从beta-9开始,缓冲区大小为8 * 1024字节,不可配置。 This will become configurable in the next release. 这将在下一版本中配置。

  • Under the hood, FileAppenders use a java.io.FileOutputStream, which may be wrapped in a BufferedOutputStream. 在引擎盖下,FileAppenders使用java.io.FileOutputStream,它可以包装在BufferedOutputStream中。 RandomAccessFileAppenders write to a ByteBuffer which is appended to the end of a RandomAccessFile when the buffer is full or flush() is called. RandomAccessFileAppenders写入ByteBuffer,当缓冲区已满或调用flush()时,ByteBuffer附加到RandomAccessFile的末尾。 There should not be much difference (apart from the performance) but FileAppenders have a longer track record and there may still be issues with the newer RandomAccessFileAppenders that haven't been discovered yet. 应该没有太大差异(除了性能)但FileAppenders有更长的跟踪记录,并且可能仍然存在尚未发现的较新的RandomAccessFileAppender的问题。 Roll-over seems to work fine for both types of appender, but there may be corner cases that the team doesn't yet know about. 翻转似乎适用于两种类型的appender,但可能存在团队尚未了解的极端情况。 (Log4J2 is under active development though and any issues will be addressed rapidly.) (Log4J2正在积极开发中,任何问题都将得到迅速解决。)

  • Both RandomAccessFileAppenders and FileAppenders can be configured with the immediateFlush attribute to flush every log event to disk. 可以使用immediateFlush属性配置RandomAccessFileAppender和FileAppender,以将每个日志事件刷新到磁盘。 I recommend you switch off immediateFlush when using AsyncAppenders or AsyncLoggers so you can leverage the nice batching behaviour they provide: the async appenders and loggers can flush after logging multiple events and will also definitely flush once when the queue is empty again, which is very efficient and at the same time ensures that all log events are always persisted to disk. 我建议您在使用AsyncAppenders或AsyncLoggers时关闭immediateFlush ,这样您就可以利用它们提供的良好批处理行为:异步appender和logger在记录多个事件后可以刷新,并且当队列再次为空时也肯定会刷新一次,这非常有效并同时确保所有日志事件始终保持为磁盘。

Random access writes direct to the file with no buffering. 随机访问直接写入文件而不进行缓冲。 This is slower but more reliable than buffered writing, in that buffered writing may lose a few unflushed writes in the event of a crash. 这比缓冲写入更慢但更可靠,因为缓冲写入可能会在发生崩溃时丢失一些未刷新的写入。 You need to decide which is more important to you. 您需要决定哪个对您更重要。 It isn't clear from your list of three. 从你的三个清单中不清楚。

EDIT One would expect that a class called RandomAccessAppender would use random access, which implies no buffering, but apparently it doesn't! 编辑人们会期望一个名为RandomAccessAppender的类会使用随机访问,这意味着没有缓冲,但显然它没有!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 log4j-找不到附加程序,自定义配置文件 - log4j - no appenders could be found, custom config file 在向同一文件创建两个追加程序时,Log4J RollingFileAppender不会滚动文件 - Log4J RollingFileAppender doesn't roll files when creating two appenders to the same file log4J的所有附加程序 - All appenders for log4J 具有多个文件附加程序的 Log4j2 json 配置 - Log4j2 json configuration with Multiple File Appenders Log4j 1和2-来自两个Log4J版本的自定义追加程序同时写入同一文件 - Log4j 1 and 2 - Custom appenders from two Log4J versions writing to the same file simultaneously 如何在运行时访问配置的 Log4J 附加程序? - How can I access the configured Log4J appenders at runtime? 可以对不同的方法使用不同的log4j追加器吗? - Possible to use different log4j appenders for different methods? log4j控制台和SMTPAppender:如何使用多个appender - log4j Console and SMTPAppender: How use multiple appenders Log4j:在运行时创建/修改appender,重新创建日志文件而不附加 - Log4j : Creating/Modifying appenders at runtime, log file recreated and not appended Log4j:如何配置多个附加程序:一个附加到控制台和一个文件,另一个附加到文件? - Log4j: How to configure multiple appenders: one directed to the console and a file and the other strictly to a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM