Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It is possible to do hourly log rotation in Winston?

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?

like image 867
Boba Fett likes JS Avatar asked Dec 18 '13 07:12

Boba Fett likes JS


People also ask

What is Winston daily rotate file?

A transport for winston which logs to a rotating file. Logs can be rotated based on a date, size limit, and old logs can be removed based on count or elapsed days.

What is Winston logger?

winston is designed to be a simple and universal logging library with support for multiple transports. A transport is essentially a storage device for your logs. Each winston logger can have multiple transports (see: Transports) configured at different levels (see: Logging levels).

How do I use a Winston logger in typescript?

The logs will be appended, multiple options are available to specify the max size, rotate the file and zip the file. import { createLogger, transports, format } from "winston"; import { createWriteStream } from "fs"; const logger = createLogger({ transports: [ new transports. Stream({ stream: createWriteStream("hello.


2 Answers

You can rotate Winston logs hourly. You need to provide hour (HH) in date pattern.

Please check the sample code below:

var winston     = require ('winston');
var path        = require ('path');

var transports  = [];

transports.push(new winston.transports.DailyRotateFile({
  name: 'file',
  datePattern: '.yyyy-MM-ddTHH',
  filename: path.join("some_path", "log_file_name.log")
}));

var logger = new winston.Logger({transports: transports});

// ... and logging
logger.info("some info log ...", {extraData: 'abc'});

File names will be as follows: log_file_name.log.2013-12-17T16, log_file_name.log.2013-12-17T17 etc.

I hope that will help.

like image 179
Tom Avatar answered Sep 23 '22 15:09

Tom


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

like image 25
Siva Kannan Avatar answered Sep 22 '22 15:09

Siva Kannan