Skip to content

Commit

Permalink
add custom date logger
Browse files Browse the repository at this point in the history
  • Loading branch information
proppy committed Jan 14, 2011
1 parent 781361c commit 30498a0
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ Specifying a specific stream:
, Log = require('log')
, log = new Log(Log.DEBUG, fs.createWriteStream('my.log'));

Specifying a specific date logging (in epoch milliseconds):
var Log = require('log')
, log = new Log(Log.DEBUG, process.stdout, { write: function(date) {
return Date.getTime();
}, read: function(s) {
return new Date(parseInt(s, 10));
}});

Instead of the log level constants, you may also supply a string:

var Log = require('log')
Expand Down
14 changes: 11 additions & 3 deletions lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ var EventEmitter = require('events').EventEmitter;
* @api public
*/

var Log = exports = module.exports = function Log(level, stream){
var Log = exports = module.exports = function Log(level, stream, date){
if ('string' == typeof level) level = exports[level.toUpperCase()];
this.level = level || exports.DEBUG;
this.stream = stream || process.stdout;
this.date = date || {
write: function(d) {
return d.toUTCString();
},
read: function(s) {
return new Date(s);
}
};
if (this.stream.readable) this.read();
};

Expand Down Expand Up @@ -117,7 +125,7 @@ Log.prototype = {
try {
var captures = line.match(/^\[([^\]]+)\] (\w+) (.*)/);
var obj = {
date: new Date(captures[1])
date: self.date.read(captures[1])
, level: exports[captures[2]]
, levelString: captures[2]
, msg: captures[3]
Expand Down Expand Up @@ -146,7 +154,7 @@ Log.prototype = {
log: function(levelStr, msg) {
if (exports[levelStr] <= this.level) {
this.stream.write(
'[' + new Date().toUTCString() + ']'
'[' + this.date.write(new Date()) + ']'
+ ' ' + levelStr
+ ' ' + msg
+ '\n'
Expand Down
67 changes: 67 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var assert = require('assert'),
fs = require('fs'),
Log = require('../lib/log'),
Stream = require('stream').Stream,
util = require('util');

var WriteStream = function() {
this.data = '';
};
util.inherits(WriteStream, Stream);
WriteStream.prototype.write = function(data) {
this.data += data;
};

var ReadStream = function(data) {
var self = this;
self.readable = true;
process.nextTick(function() {
data.split('').forEach(function(c) {
self.emit('data', c);
});
self.emit('end');
});
};
util.inherits(ReadStream, Stream);
ReadStream.prototype.setEncoding = function() {
};

module.exports = {
'test default date format ': function(beforeExit) {
var write_stream = new WriteStream(),
write_log = new Log(Log.DEBUG, write_stream);
write_log.notice('foo');

var read_stream = new ReadStream(write_stream.data),
read_log = new Log('debug', read_stream),
lines = [];

read_log.on('line', function(line){
lines.push(line);
});

beforeExit(function() {
assert.equal(1, lines.length);
assert.equal(true, lines[0].date.getTime() > 0);
});
},
'test custom date format': function(beforeExit) {
var write_stream = new WriteStream(),
custom_date = { write: function(d) { return 42; }, read: function(s) { return new Date(parseInt(s, 10)); } },
write_log = new Log(Log.DEBUG, write_stream, custom_date);
write_log.notice('foo');

var read_stream = new ReadStream(write_stream.data),
read_log = new Log('debug', read_stream, custom_date),
lines = [];
read_log.on('line', function(line){
lines.push(line);
});

beforeExit(function() {
assert.equal(1, lines.length);
assert.equal(42, lines[0].date.getTime());
});
}
};

0 comments on commit 30498a0

Please sign in to comment.