https://github.com/mnutt/redirect-stdio
Utilities for redirecting node process stdout and stderr.
https://github.com/mnutt/redirect-stdio
Last synced: 11 months ago
JSON representation
Utilities for redirecting node process stdout and stderr.
- Host: GitHub
- URL: https://github.com/mnutt/redirect-stdio
- Owner: mnutt
- License: mit
- Created: 2023-09-24T18:49:45.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-24T18:57:04.000Z (over 2 years ago)
- Last Synced: 2025-02-27T01:25:47.125Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# redirect-stdio
Utilities for redirecting node process stdout and stderr.
Note: Only works on MacOS and Linux.
## Motivation
Imagine you have a node app that logs to stdout and a bash script that
runs your node process like this:
```
node myapp.js >> access.log
```
Later, you want to add logrotate, which moves `access.log` to
`access.log.2` and then calls `SIGUSR2` on your node process. But your
node process is logging to stdout, and can't exactly "reopen"
`process.stdout`.
With `redirect-stio`, you can:
* use `pathFromFd` to find where your process's stdout is actually going
* on `SIGUSR2`, re-open the log path you just found, and write that file
descriptor over the old one to rotate logs
## Usage
```javascript
const { reopenStdout, reopenStderr } = require('redirect-stdio');
process.on('SIGUSR2', function() {
reopenStdout();
reopenStderr();
});
```
## License
Copyright 2023 Movable, Inc. See LICENSE.md.