Permalink
Browse files

Added support for time based log rotation. Logs will rotate at the ti…

…me specified in 24 hours format.
  • Loading branch information...
1 parent bfbeb87 commit c4f9bc27a3fb20b43b672edc7a242159cc5f15d3 @thakkar-rushikesh committed Apr 4, 2012
Showing with 60 additions and 25 deletions.
  1. +60 −25 lib/log.js
View
85 lib/log.js
@@ -1,34 +1,66 @@
-
/*!
* Log.js
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
+ /**
+ * Functions to support Log rotation
+ */
+function getNextDate(dt) {
+ var nxt = new Date();
+ nxt.setDate(dt.getDate()+1);
+ return nxt;
+}
+
+function calculateTimeToRotate(rotationTime) {
+ var milliSeconds;
+ if (rotationTime) {
+ var now = new Date;
+ var rotateAt = new Date;
+ if (now.getHours() >= rotationTime) {
+ rotateAt = getNextDate(now);
+ }
+ rotateAt.setHours(rotationTime);
+ rotateAt.setMinutes(0);
+ rotateAt.setSeconds(0);
+ milliSeconds = rotateAt.getTime()-now.getTime();
+ }
+ return milliSeconds;
+}
+
/**
* Module dependencies.
*/
-
var EventEmitter = require('events').EventEmitter;
/**
- * Initialize a `Loggeer` with the given log `level` defaulting
+ * Initialize a `Logger` with the given log `level` defaulting
* to __DEBUG__ and `stream` defaulting to _stdout_.
*
- * Initialize the `rotate` flag with the given value, defaulting to `false` meaning it will NOT emit `rotate` event upon date change.
+ * Initialize the `rotate` flag with the given value, defaulting to `false` meaning it will NOT emit `rotate` event.
*
* @param {Number} level
* @param {Object} stream
* @api public
*/
-var Log = exports = module.exports = function Log(level, stream, rotate){
+var Log = exports = module.exports = function Log(level, stream, userPrefs){
+
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.rotate = rotate || false;
- this.lastRotationDate = (new Date()).getDate();
+
+ this.rotate = false;
+ if(userPrefs) {
+ this.rotate = userPrefs.rotate || false;
+ this.rotationTime = userPrefs.rotationTime || 0;
+ this.timeToRotate = calculateTimeToRotate(this.rotationTime);
+
+ if (this.rotate == true)
+ this.initRotation(this.timeToRotate);
+ }
};
/**
@@ -100,6 +132,15 @@ exports.DEBUG = 7;
*/
Log.prototype = {
+ /**
+ * Initiates Log rotation timer. Emits `rotate` event after waiting till specified time.
+ */
+ initRotation: function(waitTime) {
+ var self = this;
+ setTimeout(function(){
+ self.emit('rotate', new Date);
+ }, waitTime);
+ },
/**
* Start emitting "line" events.
@@ -141,34 +182,28 @@ Log.prototype = {
/**
* Log output message.
- *
- * Emits `rotate` event when date changes.
- *
* @param {String} levelStr
* @param {Array} args
* @api private
*/
-
log: function(levelStr, args) {
if (exports[levelStr] <= this.level) {
var i = 1;
var msg = args[0].replace(/%s/g, function(){
return args[i++];
});
- var logTime = new Date;
- this.stream.write(
- '[' + logTime + ']'
- + ' ' + levelStr
- + ' ' + msg
- + '\n'
- );
- if (this.rotate) {
- if (logTime.getDate() != this.lastRotationDate) { //Rotate when date changes
- this.emit('rotate', logTime);
- this.lastRotationDate = logTime.getDate();
- }
- }
- }
+ var logTime = new Date;
+ try {
+ this.stream.write(
+ '[' + logTime.toLocaleString() + ']'
+ + ' ' + levelStr
+ + ' ' + msg
+ + '\n'
+ );
+ } catch(err) {
+ console.log(err);
+ }
+ }
},
/**

0 comments on commit c4f9bc2

Please sign in to comment.