# logrotate-stream

> Pipe log data to a stream, fuggetabout rotation

Latest version **0.2.9** (published 2023-02-14) · BSD-2-Clause license · 0 weekly downloads

## Install

```sh
npm install logrotate-stream
pnpm add logrotate-stream
yarn add logrotate-stream
bun add logrotate-stream
```

Provides the command `logrotate-stream`.

## Health

**Score 33/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.2.9 |
| Published | 2023-02-14 |
| First published | 2013-11-13 |
| Weekly downloads | 0 |
| License | BSD-2-Clause |
| TypeScript types | separate (@types/logrotate-stream) |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 11.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 38 |
| Author | Drew Stokes |
| Maintainers | dstokes |
| Keywords | log, rotate, pipe, stream |

## Links

- npm: https://www.npmjs.com/package/logrotate-stream
- Repository: https://github.com/dstokes/logrotate-stream
- Homepage: https://github.com/dstokes/logrotate-stream#readme
- Issues: https://github.com/dstokes/logrotate-stream/issues
- npm.io page: https://npm.io/package/logrotate-stream

## Dependencies (3)

- [byt](https://npm.io/package/byt.md) ~0.1.0
- [yargs](https://npm.io/package/yargs.md) ~17.6.2
- [log-rotate](https://npm.io/package/log-rotate.md) ~0.2.8

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 0.2.9 (latest) — 2023-02-14
- 0.2.8 — 2021-04-06
- 0.2.7 — 2020-08-15
- 0.2.6 — 2020-03-25
- 0.2.5 — 2015-02-13
- 0.2.4 — 2015-01-26
- 0.2.3 — 2014-01-29
- 0.2.2 — 2013-11-19
- 0.2.1 — 2013-11-16
- 0.2.0 — 2013-11-15
- 0.1.0 — 2013-11-14
- 0.0.2 — 2013-11-13
- 0.0.1 — 2013-11-13

## README

logrotate-stream
================

A Writable Stream that supports linux logrotate style options

[![Build Status](https://travis-ci.org/dstokes/logrotate-stream.png)](https://travis-ci.org/dstokes/logrotate-stream)
[![Donate](http://img.shields.io/bitcoin/donate.png?color=blue)](https://www.coinbase.com/dstokes)  
[![NPM](https://nodei.co/npm/logrotate-stream.png?downloads=true)](https://nodei.co/npm/logrotate-stream/)  

example
=======
On the command line:
``` sh
  node app.js 2>&1 | logrotate-stream app.log --keep 3 --size '50m' --compress
```

As a module:
``` js
var stream = require('logrotate-stream')
  , toLogFile = stream({ file: './test.log', size: '100k', keep: 3 });

someStream.pipe(toLogFile);
```

the problem
===========
Rotating logs that are being written to with stdio redirection sucks. Using a
utility like `logrotate` doesn't automagically update your processes log file 
descriptor and you end up with several empty logs and one mega rotated log.

There's a couple ways to try and deal with this, but they all fall short:
#### 1. Use `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 a process signal

Often times, production apps can't be restarted willy-nilly

#### 3. Use the `copytruncate` feature of `logrotate`

This only works if you don't need to guarantee that all of your log lines are
persisted. `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.

`logrotate-stream` tries to remedy this situation by acting as an intermediary
between the application and the file system, piping `stdin` to log files and
rotating those logfiles when necessary.

upstart woes
============
If you find yourself using logrotate-stream with upstart, there's a few things
to consider. Piping to logrotate-stream in your `exec` line will cause upstart
to track the pid of the logrotate process rather than your app. While stopping
will still work (most likely emitting an EPIPE error on your app before
exiting), it would be better if you used a named pipe to redirect your apps output:
```
chdir /path/to/app

pre-start script
  # create a named pipe
  mkfifo logpipe
  # create a backgrounded logrotate-stream process and
  # redirect the named pipe data to it
  logrotate-stream app.log --keep 3 --size 50m < logpipe &
end script

# start the app, redirecting stdout & stderr to the named pipe
exec /usr/local/bin/node index.js > logpipe 2>&1
```

This setup will register the correct pid with upstart, make sure your stdio
is forwarded to logrotate-stream, and will properly kill the logrotate-stream
process when your app is stopped.

options
=======

### file
The file log file to write data to.

### size
The max file size of a log before rotation occurs. Supports `1024`, `1k`, `1m`, `1g`

### keep
The number of rotated log files to keep (including the primary log file). 
Additional logs are deleted no rotation.

### compress
Optionally compress rotated files with gzip.

install
=======

With [npm](http://npmjs.org) do:

```
npm install logrotate-stream
```

---
_Source: https://npm.io/package/logrotate-stream · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
