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.