What is the difference between RollingFile and RollingRandomAccessFile in log4j2 configuration

11,838

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.)

Share:
11,838

Related videos on Youtube

Nitin Khanna
Author by

Nitin Khanna

Updated on September 04, 2022

Comments

  • Nitin Khanna
    Nitin Khanna over 1 year

    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

  • devwebcl
    devwebcl over 5 years
    so, practically the difference is RandomAccessFile vs OutputStream, and buffer size by default ?