Thursday, July 25, 2013

Node.js - Winston - Rotating File Logger with Dynamic Method Name



if you use node.js, you know winston library. If you use winston to log files - you always wanted a rotating file logger with dynamic naming support (e.g. each file has date).

if so - check out my latest pull request: https://github.com/flatiron/winston/pull/273
This will make the files created under "log" to be named:
run.log
run20130629190057.log
and so forth...

The goal of the rotation format property is to allow the user to pass a pointer to a function that will determine the format of the file name in case of a rotating logger (a logger with maxsize that generated files).

sample usage:

so these would be files created today (
2013-06-29-190056):
run20130629190056.log

sample code:

"winston.add(winston.transports.File, { 
filename: 'log/run.log',

maxsize: 150,
rotationFormat: function() {
return getFormattedDate();
function getFormattedDate() {
var temp = new Date();
return dateStr = padStr(temp.getFullYear()) +
padStr(1 + temp.getMonth()) +
padStr(temp.getDate()) +
padStr(temp.getHours()) +
padStr(temp.getMinutes()) +
padStr(temp.getSeconds());

}
function padStr(i) {
return (i < 10) ? "0" + i : "" + i;
}
}
});"

No comments:

Post a Comment