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.34k stars 1.59k forks source link

Consider replacing `FileOutputStream` with `Files.newOutputStream` #2117

Open ppkarwasz opened 8 months ago

ppkarwasz commented 8 months ago

Since Java 7 two methods are available to open new files:

While they mostly perform the same OS operations, there is a significant difference between the two on Windows: the IO method opens the file without the FILE_SHARE_DELETE option, whereas the NIO method adds the FILE_SHARE_DELETE option (cf. this SO question).

Since log files on UNIX can already be deleted without stopping the application, this change would allow an alignment between the behavior of Log4j on Windows and UNIX.

I discussed this with @garydgregory on Slack.

vy commented 8 months ago

There are more (platform-independent!) hazards surrounding FileOutputStream::new. Please indeed replace them with Files::newOutputStream.

jvz commented 8 months ago

I like this idea because it also supports use of other file system implementations based on what the user has installed. That could also be useful in tests if we used something like https://github.com/google/jimfs for in-memory file system testing.