log4j2配置中的RollingFile和RollingRandomAccessFile有什么区别?[英] What is the difference between RollingFile and RollingRandomAccessFile in log4j2 configuration






主要区别是性能: .

RandomAccessFileAppender始终被缓冲,而FileAppender提供了一个配置开关(Bufferedio).两者都有一个"立即填充"配置选项,以防您要确保您的消息在磁盘上(例如审核日志记录).最后,随机cessfileappender的默认缓冲区大小较大:256*1024字节vs 8*1024字节的fileappender(两个附录均可在配置中设置缓冲尺寸).


另外一件事是,基础实现在一种情况下使用随机捕获器,而在另一种情况下则使用一个输出流(如名称所暗示的那样).将RandomAccessFileAppender与Unix Logrotate实用程序相结合( .org/jira/browse/log4j2-354 ),log4j团队建议您使用RollingRandomAccessfile进行滚动,并避免使用Unix logrotate实用程序.显然,丝脂可以与Logrotate结合使用 - 我们没有听说过任何问题. (由于问题已经提到了滚动...对于两个附录的变体,您已经在做正确的事情,所以我只是认为我应该提到它.)



In our current implementation one of the servers has the configuration for log4j2 set for RollingRandomAccessFile and in the other RollingFile. I would like to know what is the difference between the two and pros and cons of each if possible.



The main difference is performance: http://logging.apache.org/log4j/2.x/manual/async.html#FileAppender_vs._RandomAccessFileAppender

RandomAccessFileAppender is always buffered, while FileAppender provides a config switch (bufferedIO). Both have an "immediateFlush" config option in case you want to be sure your messages are on disk (e.g. audit logging). Finally, the default buffer size for RandomAccessFileAppender is larger: 256*1024 bytes vs 8*1024 bytes for FileAppender (both appenders buffer size can be set in configuration).

One more thing, the underlying implementation uses a RandomAccessFile in one case and an OutputStream in the other case (as the name suggests). There is a known issue with combining RandomAccessFileAppender with the unix logrotate utility (https://issues.apache.org/jira/browse/LOG4J2-354), and the Log4j team recommends you use the RollingRandomAccessFile for rollover and avoid use of the unix logrotate utility. Apparently FileAppender can be combined with logrotate - we haven't heard of any issues. (Since the question already mentions the Rolling... variant for both appenders you are already doing the right thing, I just thought I should mention it.)