@wuyuchentr/log-rotator
v1.0.0
Published
Auto-rotate log files by size or time. Supports compression and polling watch.
Downloads
96
Maintainers
Readme
@wuyuchentr/log-rotator
Auto-rotate log files by size or time. Supports compression and polling watch. Zero dependencies (uses built-in zlib).
A lightweight
logrotatefor Node.js apps.
Install
npm install @wuyuchentr/log-rotatorUsage
const { rotateLog, watchLog } = require('@wuyuchentr/log-rotator');One-shot rotation
const result = await rotateLog('app.log', {
maxSize: '10MB',
maxFiles: 5,
compress: true,
copytruncate: false, // default: rename + create empty file
});
// → { rotated: true }Polling watch
const watcher = watchLog('app.log', {
maxSize: '10MB',
maxFiles: 3,
compress: true,
interval: '30s', // check every 30 seconds
onError: console.error,
});
// later...
watcher.stop();Options
| Option | Default | Description |
|--------|---------|-------------|
| maxSize | — | Rotate when file exceeds this ('10MB', '1GB', '500KB') |
| maxFiles | 5 | Number of rotated files to keep |
| compress | false | Gzip rotated files (app.log.1.gz) |
| copytruncate | false | Copy + truncate instead of rename (safer for apps that don't reopen logs) |
| interval | 60000 | Poll interval for watchLog() ('30s', '5m', '1h') |
File naming
app.log → current log
app.log.1 → 1st backup (→ app.log.1.gz if compress)
app.log.2 → 2nd backup
...
app.log.N → Nth backup (deleted when N > maxFiles)How it works
- Stats the log file
- If
size >= maxSize, shifts backups:.N → .N+1,.1 → .2, etc. - Renames (or copy/truncates) current log to
.1 - Creates a new empty log file
- Gzips
.1ifcompress: true
