Pipe stdin to a logfile with optional log rotation parameters
node index.js 2>&1 | log-pipe app.log --count 3 --lines 15 --compress
Log rotation with stdio redirection sucks. When using a utility like
to rotate application logs that are written to with standard io redirection
./app &> app.log the process continues to write to
rotated logs, leaving your primary log empty after rotaion. This is due to the
fact that the file descriptor is not updated when its underlying log is moved.
There's a couple ways to try and deal with this, but they all fall short:
winston's log rotation feature for nodejs apps
This requires adding a new dependency and possibly code changes around logging logic.
2. Restart your app on
Often times, production apps can't be restarted willy-nilly
3. Use the
copytruncate feature of
This only works if you don't need to guarantee that all of your log lines are
copytruncate performs a non-atomic copy before truncating the
original log, which means you can lose data in the process if the copy is slow.
log-pipe tries to solve this by acting as an intermediary between the application
and the file system, piping
stdin to log files and rotating those logfiles
The maximum number of rotated logs (including primary log file). Additional
count will be deleted.
The maximum number of log lines before a rotation occurrs.
Compress rotated files with gzip
With npm do:
npm install log-pipe