pinojs / sonic-boom

Extremely fast utf8 only stream implementation
MIT License
266 stars 41 forks source link

Add append option to open file in write mode (truncate) instead of append mode #76

Closed mojavelinux closed 3 years ago

mojavelinux commented 3 years ago

Users of my application, which uses pino for logging, want to be able to clean the log file before the application starts writing to it. Currently, pino can only append to the file because SonicBoom always opens the file in append (a) mode. As a result, the onus falls on my application to remove the file before instantiating pino (and hence SonicBoom).

This situation is less than ideal because the application has to inspect the logging options, determine if there is a file to remove, do the work of removing the file, and handle any errors that comes from it. (If the function that instantiates the logger is not an async function, this has to be done using a sync fs call). That complexity could be somewhat mitigated if there were a hook in SonicBoom before the file is opened, but there's currently no such hook.

I'm proposing to make this behavior configurable, controlled by a new append option. If the value of the option is false or undefined, SonicBoom will open the file in append mode as it currently does (hence, backwards compatible). If the value of the option is true, SonicBoom will open the file in write mode, which effectively truncates the contents.

Here's how this option would be used:

new SonicBoom({ dest: 'application.log', append: false })

If this proposal is acceptable, I will do the work of submitting a PR with tests to implement it.

mcollina commented 3 years ago

Send the PR!

mojavelinux commented 3 years ago

On it! :runner: