2

Is there any way to implement file appender using log4j2, which will store the log in memory for particular size and after that it will write the log to the file(local/remote).

Is there any way to achieve this?

In log4j2 documentation:

The FastFileAppender is similar to the standard FileAppender except it is always buffered (this cannot be switched off) and internally it uses a ByteBuffer + RandomAccessFile instead of a BufferedOutputStream. We saw a 20-200% performance improvement compared to FileAppender with "bufferedIO=true"

bufferedIO:(boolean) When true - the default, records will be written to a buffer and the data will be written to disk when the buffer is full or, if immediateFlush is set, when the record is written. File locking cannot be used with bufferedIO. Performance tests have shown that using buffered I/O significantly improves performance, even if immediateFlush is enabled.

immediateFlush:(boolean) When set to true - the default, each write will be followed by a flush. This will guarantee the data is written to disk but could impact performance. Flushing after every write is only useful when using this appender with synchronous loggers. Asynchronous loggers and appenders will automatically flush at the end of a batch of events, even if immediateFlush is set to false. This also guarantees the data is written to disk but is more efficient.

By using above attributes we can control the writing data to the file, But I didn't find the way to store the log in memory and then log it after reaching particular buffer size.

Update: Added new feature request in LOG4J2-jira-project: LOG4J2-401.

Chandra Sekhar
  • 16,256
  • 10
  • 67
  • 90

1 Answers1

1

As of the latest beta (beta-9, to be released in a few days), users cannot control the buffer size for FileAppender or RandomAccessFileAppender. (FastFileAppender has been renamed to RandomAccessFileAppender in beta-9.)

FYI, the buffer size of FileAppender is 8192 bytes, for RandomAccessFileAppender the buffer size is 262,144 bytes.

If you need to control buffer size, please raise a feature request on either the log4j2 issue tracker or the user mailing list.


Update: LOG4J2-401 was fixed in RC1. Since RC2, the buffer size is also configurable for RollingRandomAccessFileAppender and RollingFileAppender.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114