apache / logging-log4j2

Apache Log4j 2 is a versatile, feature-rich, efficient logging API and backend for Java.
https://logging.apache.org/log4j/2.x/
Apache License 2.0
3.4k stars 1.62k forks source link

Add file appender option to flush OS buffers #2023

Open tversic opened 11 months ago

tversic commented 11 months ago

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!

jvz commented 11 months ago

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 >_>

tcmot commented 10 months ago

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.

ppkarwasz commented 10 months ago

@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 commented 10 months ago

Issue #2117 is related to this.