It is possible to do hourly log rotation in Winston?

Viewed 6264

Hi i have a requirement from our production team, I need to create the logs hourly, I know that winston support daily, but this doesn't help me. It is possible to do this?

2 Answers

UPDATED As @Tom has mentioned rotating Logs has moved out of winston and loaded if required

npm install winston-daily-rotate-file

Code sample

const winston = require('winston')
require('winston-daily-rotate-file');
const path = require('path');
let transports  = [];
const { createLogger } = winston;
transports.push(
    new winston.transports.DailyRotateFile({
      name: 'file',
      datePattern: 'YYYY-MM-DD-THH-mm',
      filename: path.join(__dirname, 'rotate_logs', 'log_file.log')
    })
)
var logger = createLogger({ transports: transports })

Full Example if want to test the above code

dataLog(0)
function dataLog(secondsPassed){
    setTimeout(function(){
        let dateNow = new Date();
        logger.info(`seconds passed ${secondsPassed} and Time is ${dateNow}`);
        console.log(`${secondsPassed}`);
        if(dataLog != 130){ //when reaches 130 seconds stops logging
              dataLog(++secondsPassed);
        }
    },1000);
}

The result files mentioned in the attached image

Result of rotating logs

EXTRA: I have created winston examples with different use cases, Might be helpful https://github.com/shivashanmugam/node-lab/blob/master/winston/index.js

Related