Permalink
Browse files

Implemented Date-based rotation.

  • Loading branch information...
1 parent 75ca71b commit bfbeb87acdd6dee684c58f19a9a2134cbda103e7 @thakkar-rushikesh committed Mar 13, 2012
Showing with 17 additions and 13 deletions.
  1. +17 −13 lib/log.js
View
@@ -1,3 +1,4 @@
+
/*!
* Log.js
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
@@ -14,20 +15,20 @@ var EventEmitter = require('events').EventEmitter;
* Initialize a `Loggeer` with the given log `level` defaulting
* to __DEBUG__ and `stream` defaulting to _stdout_.
*
- * Initialize the `rotateLimit` with the given value, defaulting to 10,000 lines (inclusive).
+ * Initialize the `rotate` flag with the given value, defaulting to `false` meaning it will NOT emit `rotate` event upon date change.
*
* @param {Number} level
* @param {Object} stream
* @api public
*/
-var Log = exports = module.exports = function Log(level, stream){
+var Log = exports = module.exports = function Log(level, stream, rotate){
if ('string' == typeof level) level = exports[level.toUpperCase()];
this.level = level || exports.DEBUG;
this.stream = stream || process.stdout;
if (this.stream.readable) this.read();
- this.rotateLimit = rotateLimit || 10000;
- this.totalLogged = 0;
+ this.rotate = rotate || false;
+ this.lastRotationDate = (new Date()).getDate();
};
/**
@@ -99,7 +100,7 @@ exports.DEBUG = 7;
*/
Log.prototype = {
-
+
/**
* Start emitting "line" events.
*
@@ -140,7 +141,8 @@ Log.prototype = {
/**
* Log output message.
- * Emit `rotate` event after the number of logged-lines cross `rotateLimit` (specified while initializing)
+ *
+ * Emits `rotate` event when date changes.
*
* @param {String} levelStr
* @param {Array} args
@@ -153,18 +155,20 @@ Log.prototype = {
var msg = args[0].replace(/%s/g, function(){
return args[i++];
});
+ var logTime = new Date;
this.stream.write(
- '[' + new Date + ']'
+ '[' + logTime + ']'
+ ' ' + levelStr
+ ' ' + msg
+ '\n'
);
- var i = ++this.totalLogged;
- if (i >= this.rotateLimit) {
- this.emit('rotate');
- this.totalLogged = 0;
- }
- }
+ if (this.rotate) {
+ if (logTime.getDate() != this.lastRotationDate) { //Rotate when date changes
+ this.emit('rotate', logTime);
+ this.lastRotationDate = logTime.getDate();
+ }
+ }
+ }
},
/**

0 comments on commit bfbeb87

Please sign in to comment.