Log Rotation
Manage log file sizes and retention
Automatically manage log file sizes, compression, and retention policies.
Basic Configuration
logixlysia({
config: {
logFilePath: './logs/app.log',
logRotation: {
maxSize: '10m', // Rotate when file reaches 10MB
maxFiles: '7d', // Keep logs for 7 days
compress: true // Compress rotated logs
}
}
})
Rotation Options
Size-based Rotation
logRotation: {
maxSize: '10m' // 10 megabytes
}
Supported formats: '1k', '1m', '1g' or bytes (number)
Time-based Rotation
logRotation: {
interval: '1d' // Rotate when the live file is a day old
}
Supported formats: a number plus a unit — '1h' (hours), '1d' (days), '1w' (weeks). Minutes and bare numbers are not accepted.
Retention Policy
logRotation: {
maxFiles: '7d' // Keep logs for 7 days
}
Or keep a specific number of files:
logRotation: {
maxFiles: 10 // Keep last 10 files
}
Compression
logRotation: {
compress: true // Enable compression (gzip)
}
How Rotation Works
Rotation happens on write, when either configured trigger is crossed: the in-memory byte count exceeds maxSize, or the live file’s age (from filesystem creation time) has reached interval. interval is checked before a write batch, so the first line written after it elapses goes into the new file, and maxSize is checked after one. Neither runs on a timer. An empty file is not rotated. If its interval has already elapsed when a line arrives, the running process counts the period from that write; after a restart the age is measured from the file’s creation time again.
When a log file is rotated, it’s renamed with a timestamp and a high-resolution counter to guarantee uniqueness:
app.log → app.log.2026-07-27-14-30-05-123-45678901234567
The suffix is <YYYY-MM-DD-HH-MM-SS-SSS>-<hrtime>. If compression is enabled, a .gz extension is appended after rotation:
app.log.2026-07-27-14-30-05-123-45678901234567 → app.log.2026-07-27-14-30-05-123-45678901234567.gz
Retention
maxFiles controls how many rotated files are kept:
- A number (e.g.
10) keeps that many most-recent rotated files, deleting older ones. It must be a positive integer;0or a negative value is rejected at startup. - A string (e.g.
'7d') keeps rotated files younger than that age, deleting older ones. The age is counted from the rotated file’s modification time: the last line written to it, or the moment it was compressed. Whenintervalrotates a file that sat idle for longer than that age, the file is therefore deleted at once, unlesscompresshas just written a compressed copy of it.
Example Configurations
Production
logRotation: {
maxSize: '100m',
maxFiles: '30d',
compress: true,
interval: '1d'
}
Development
logRotation: {
maxSize: '10m',
maxFiles: '7d',
compress: false
}
High-Volume
logRotation: {
maxSize: '1g',
maxFiles: '7d',
compress: true,
interval: '1h'
}
Important Notes
- Empty files are not rotated
- Rotation failures don’t crash the application
- Compression runs asynchronously
- Old files are automatically cleaned up based on
maxFiles - Interval rotation is checked on write only; an idle process rotates on its next write