Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winston: how to rotate logs

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' }); 
like image 677
Sparky1 Avatar asked Jul 09 '12 22:07

Sparky1


People also ask

What is rotating a log file?

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).

How do I change the log level in Winston?

If you are using the default logger, you can adjust the log levels like this: const winston = require('winston'); // ... winston.

How do I view Winston logs?

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.


2 Answers

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:

  1. The File transport accepts a maxsize option which will rotate the logfile when it exceeds a certain size in bytes.

  2. 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

like image 110
indexzero Avatar answered Sep 28 '22 08:09

indexzero


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         })     ] }); 
like image 32
Muki Buki Avatar answered Sep 28 '22 08:09

Muki Buki