What is winston-daily-rotate-file?
The winston-daily-rotate-file npm package is a transport plugin for the winston logger that outputs log messages to a file, rotating the file automatically based on time, size, or both. It is commonly used to manage log files, ensuring they are kept to a manageable size and are rotated out over time for archival purposes.
What are winston-daily-rotate-file's main functionalities?
Time-based log rotation
This feature allows logs to be rotated daily. The '%DATE%' pattern in the filename is replaced with the current date in the 'YYYY-MM-DD' format. Logs older than 14 days are deleted, and logs are zipped when they are rotated.
const winston = require('winston');
const DailyRotateFile = require('winston-daily-rotate-file');
const logger = winston.createLogger({
transports: [
new DailyRotateFile({
filename: 'application-%DATE%.log',
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d'
})
]
});
Size-based log rotation
This feature allows logs to be rotated when they reach a certain size. In this example, the log file is rotated when it reaches 20 megabytes. The 'maxFiles' option is set to 5, which means that a maximum of 5 rotated log files will be kept.
const winston = require('winston');
const DailyRotateFile = require('winston-daily-rotate-file');
const logger = winston.createLogger({
transports: [
new DailyRotateFile({
filename: 'application-%DATE%.log',
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: '5'
})
]
});
Customizable file compression
This feature allows the rotated log files to be compressed using gzip. The 'zippedArchive' option is set to true, enabling this functionality.
const winston = require('winston');
const DailyRotateFile = require('winston-daily-rotate-file');
const logger = winston.createLogger({
transports: [
new DailyRotateFile({
filename: 'application-%DATE%.log',
datePattern: 'YYYY-MM-DD',
zippedArchive: true
})
]
});
Other packages similar to winston-daily-rotate-file
rotating-file-stream
This package provides a stream that writes logs to a file and rotates it based on time or size, similar to winston-daily-rotate-file. It is not tied to any specific logging library and can be used with any Node.js stream-compatible logging tool.
file-stream-rotator
Similar to winston-daily-rotate-file, this package is used to rotate logs on a time basis. It does not depend on winston and can be used independently to manage log file rotation in any Node.js application.
bunyan-rotating-file-stream
This package is a rotating file stream for the Bunyan logging library. It offers functionality similar to winston-daily-rotate-file but is specifically designed to work with Bunyan instead of winston.
winston-daily-rotate-file
I will be continuing work on winston
itself with gusto, but do not plan on spending cycles on maintaining this transport. This was the primary motivation for breaking it out from the core in winston@2.0.0
. If you are interested please let me know in the tracking Github issue.
Usage
A transport for winston which logs to a rotating file each day.
winston.add(require('winston-daily-rotate-file'), options)
The DailyRotateFile transport can rotate files by minute, hour, day, month or year. Its options are identical to the File transport with the lone addition of the 'datePattern' option:
- datePattern: A string representing the pattern to be used when appending the date to the filename (default '.yyyy-MM-dd'). The meta characters used in this string will dictate the frequency of the file rotation. For example if your datePattern is simply '.HH' you will end up with 24 log files that are picked up and appended to every day.
Valid meta characters in the datePattern are:
- yy: Last two digits of the year.
- yyyy: Full year.
- M: The month.
- MM: The zero padded month.
- d: The day.
- dd: The zero padded day.
- H: The hour.
- HH: The zero padded hour.
- m: The minute.
- mm: The zero padded minute.
Metadata: Logged via util.inspect(meta);
LICENSE: MIT