How can I rotate logs when using Winston to handle logging for node.js. That is, how can I create a new file for each day the app runs?
var logger = new (winston.Logger)({ transports: [ new (winston.transports.Console)(), new (winston.transports.File)({ filename: '2012-07-09.log' }) ] }); logger.log('info', 'Test Log Message', { anything: 'This is metadata' });
In information technology, log rotation is an automated process used in system administration in which log files are compressed, moved (archived), renamed or deleted once they are too old or too big (there can be other metrics that can apply here).
If you are using the default logger, you can adjust the log levels like this: const winston = require('winston'); // ... winston.
Go to the example. log file in the logs folder to view the log. Winston allows you to implement multiple logging transports, i.e., a log can be recorded to a file, console, or database. The Logger configuration below logs to a console and a file.
winston author and maintainer here.
Logging to a new file everyday is currently an open feature request: https://github.com/flatiron/winston/issues/10. Would love to see someone implement it.
That said, there are other options:
The File transport accepts a maxsize option which will rotate the logfile when it exceeds a certain size in bytes.
There is also an open pull-request with a new transport I haven't had a chance to really dig into "fileRotate", which it seems does this kind of date-based rotation: https://github.com/flatiron/winston/pull/120/files
The feature is present and we are using it in production, winston.transports.DailyRotateFile:
var timeFormatFn = function() { 'use strict'; return moment().format(cfg.timeFormat); }; var logger = new(winston.Logger)({ exitOnError: false, transports: [ new(winston.transports.DailyRotateFile)({ filename: cfg.appLogName, dirname: __dirname + '/../' + cfg.logsDirectory, datePattern: cfg.rollingDatePattern, timestamp: timeFormatFn }), new(winston.transports.Console)({ colorize: true, timestamp: timeFormatFn }) ] });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With