问题描述
在我们当前的实现中,其中一个服务器具有用于rollingRandomAccessfile和另一个RollingFile的Log4J2设置的配置.我想知道两者之间有什么区别,如果可能
谢谢
推荐答案
另外一件事是,基础实现在一种情况下使用随机捕获器,而在另一种情况下则使用一个输出流(如名称所暗示的那样).将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.
Thanks
推荐答案
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.)