Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add custom date logger

  • Loading branch information...
commit 30498a0c7ef060f26eb4bb31ec25b987e3161f57 1 parent 781361c
@proppy proppy authored
Showing with 86 additions and 3 deletions.
  1. +8 −0 Readme.md
  2. +11 −3 lib/log.js
  3. +67 −0 test/test.js
View
8 Readme.md
@@ -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')
View
14 lib/log.js
@@ -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();
};
@@ -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]
@@ -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'
View
67 test/test.js
@@ -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());
+ });
+ }
+};
+
Please sign in to comment.
Something went wrong with that request. Please try again.