Minel Aydın
Minel Aydın

Reputation: 137

How to do logrotate logs into separate files daily in Node.js using Winston library

I have a nodejs app. I need to log each event in this program and I use winston library for this. I am trying to create a log file for each day.

All logs should be separated day by day. Those file's names should be the date of the current day.

For example today I open my program and do a get request. This action should be saved as a file on my computer ( eg on my desktop) and the file's name should be 17/10/2019.log. Any suggestion?

EDIT: SOLVED!!

require('winston-daily-rotate-file');
const logDir= 'C://Users/Desktop/LogFiles';

var options = {
         file: {
           level:'info',
           filename: path.resolve(`${logDir}/${new Date().getFullYear().toString()} - ${new Date().getMonth()+1}/%DATE%.log`),
           datePattern: 'YYYY-MM-DD',
           timestamp: new Date()
          };


     let logger = winston.createLogger({
      level:'info', 
      format: winston.format.combine(
        winston.format.printf(info => { return `${info.timestamp} || ${info.level} || Message: ${info.message}`; })
 ),

     transports: [
       new winston.transports.DailyRotateFile(options.file)
        ],  
           exitOnError: false,
    });

Upvotes: 0

Views: 2279

Answers (1)

B.Mouad
B.Mouad

Reputation: 151

you could use a Transport for Winston, checkout https://github.com/winstonjs/winston-daily-rotate-file

it can do all what you're asking for and more.

Upvotes: 1

Related Questions