A Brief Introduction Of Log Rotation

Posted By : Vipin Pokhriyal | 04-Apr-2018

 

 

I am explaining here how to rotate log into Node js:-

1)Using Pm2.

2)Using Winston Daily rotate file.

 

1)Using Pm2

PM2 module rotate logs using pm2 logrotate, Firstly you have to install logrotate:

pm2 install pm2-logrotate

For a specific version, you can use @<version> suffix

pm2 install pm2-logrotate@2.2.0

 

 

Configuration:

 

 

You can configure these values

  i)max_size(Default 10M)

  ii)retain (Default all)

  iii)compress (Default false)

  iv)dateFormat (Default YYYY-MM-DD_HH-mm-ss)

  v)rotateModule(Default true)

  vi)workerInterval (Default 30 in secs)

  vii)rotateInterval (Default 0 0 * * * )

  viii)TZ (Default system time)

 

 

 

Example to set these values:

  pm2 set pm2-logrotate:retain 12

  pm2 set pm2-logrotate:compress false

  pm2 set pm2-logrotate:dateFormat YYYY-MM-DD_HH-mm-ss

  pm2 set pm2-logrotate:max_size 1M

  pm2 set pm2-logrotate:rotateInterval '0 * * * * '

  pm2 set pm2-logrotate:rotateModule true

  pm2 set pm2-logrotate:workerInterval 15

 

 

 

2)Using Winston-daily-rotate-file

 

 

The DailyRotateFile transport file for every minute, hour, day, month, year, weekday.

DailyRotateFile transport accept following option:-
  i)datePattern:- Its append date to the filename if your date pattern simple HH you will end up with 24 log files.

  ii)prepend:-rolling time of the log file prepended at the beginning of the filename.

  iii)local time:-By default false used UTC time.

  iv)zipped archive(default false)

  v)maxDays: Define maximum Number of days a log file will be removed, any log file older of this will be removed.

  vi)createTree


Valid date patterns are:

  yy: Its represent Last two digits of the year

  yyyy: Its represent the Full year.

  MM: represent the zero padded month.

  D: Represent The day.

  dd: Its a zero-padded day.

  H: Represent The hour.

  HH: Represent The zero-padded hour.

  M: Its represent The minute.

  Mm: Its represent The zero padded minute.

  Ddd: Its represent The weekday.

 

 

Example:

//Converting time into IST
var IST = function() {
return moment().tz('asia/kolkata').format('DD-MM-YYYY HH:mm:ss:SSS Z');
};

Logger = new(winston.Logger)({
transports: [
new(winston.transports.Console)({
handleExceptions: true,
localTime: true,
json: false,
timestamp: IST
}),
new(winston.transports.DailyRotateFile)({
filename: logDirectory + '/.log',
datePattern: 'dd-MM-yyyy.HH',
prepend: true,
localTime: true,
size: '5M',
timestamp: IST
})
],
exitOnError: false
});

 

 

 

About Author

Author Image
Vipin Pokhriyal

Vipin is Qualified in Master in computer Applicatins. He is an Active team player & having skills as Java Developer.

Request for Proposal

Name is required

Comment is required

Sending message..