logging-log4j2 icon indicating copy to clipboard operation
logging-log4j2 copied to clipboard

Add file appender option to flush OS buffers

Open tversic opened this issue 1 year ago • 4 comments

Hey guys, I have already asked this question on the discussion page and it seems like that post isnt getting any reach so I am writing this issue here again, I hope that is not problem and that someone will be able to clear things out a little for me.

Since writing same thing twice doesnt make much sense I will be linking discussion below, so someone can take a look and comment if there is any idea on the problem.

issue: https://github.com/apache/logging-log4j2/discussions/2004

Thank you!

tversic avatar Dec 01 '23 10:12 tversic

For one, I just noticed that immediateFlush isn't even used in RollingFileAppender despite being an option. Let's see what else is broken because of this >_>

jvz avatar Dec 01 '23 17:12 jvz

Hi,@jvz

RandomAccessFile 

FileChannel channel = randomAccessFile.getChannel();
randomAccessFile.write();
channel.force(true);

Use the force method,the file be updated in real time. But affects performance. Log4j2,not use the force method. Rewrite RollingRandomAccessFileManager and RandomAccessFileManager flush method. Add the code above. Or add the above code when rolling over the file.

tcmot avatar Dec 26 '23 01:12 tcmot

@tcmot,

Thanks for the suggestion. Your solution will cause two system calls (a write and an fsync/fdatasync on Linux) to be issued for each log event. I would rather use:

These have the advantage to delegate the syncing part to the OS, which can do it much more efficiently.

Of course such a behavior should be optional, since most users don't care when the OS synchronizes its I/O buffers with the disk storage.

ppkarwasz avatar Dec 26 '23 12:12 ppkarwasz

Issue #2117 is related to this.

ppkarwasz avatar Dec 26 '23 17:12 ppkarwasz