Skip to content

Commit d963983

Browse files
committed
Merge branch 'reader'
2 parents a6035b8 + c9b1e66 commit d963983

File tree

3 files changed

+68
-14
lines changed

3 files changed

+68
-14
lines changed

examples/file.log

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
[Sun, 26 Sep 2010 01:10:23 GMT] DEBUG a debug message
2-
[Sun, 26 Sep 2010 01:10:23 GMT] INFO a info message
3-
[Sun, 26 Sep 2010 01:10:23 GMT] NOTICE a notice message
4-
[Sun, 26 Sep 2010 01:10:23 GMT] WARNING a warning message
5-
[Sun, 26 Sep 2010 01:10:23 GMT] ERROR a error message
6-
[Sun, 26 Sep 2010 01:10:23 GMT] CRITICAL a critical message
7-
[Sun, 26 Sep 2010 01:10:23 GMT] ALERT a alert message
8-
[Sun, 26 Sep 2010 01:10:23 GMT] EMERGENCY a emergency message
1+
[Sun, 26 Sep 2010 01:26:14 GMT] DEBUG a debug message
2+
[Sun, 26 Sep 2010 01:26:14 GMT] INFO a info message
3+
[Sun, 26 Sep 2010 01:26:14 GMT] NOTICE a notice message
4+
[Sun, 26 Sep 2010 01:26:14 GMT] WARNING a warning message
5+
[Sun, 26 Sep 2010 01:26:14 GMT] ERROR a error message
6+
[Sun, 26 Sep 2010 01:26:14 GMT] CRITICAL a critical message
7+
[Sun, 26 Sep 2010 01:26:14 GMT] ALERT a alert message
8+
[Sun, 26 Sep 2010 01:26:14 GMT] EMERGENCY a emergency message

examples/reader.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var Log = require('../lib/log')
7+
, fs = require('fs')
8+
, stream = fs.createReadStream(__dirname + '/file.log')
9+
, log = new Log('debug', stream);
10+
11+
log.on('line', function(line){
12+
console.log(line);
13+
});;

lib/log.js

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11

22
/*!
3-
* Log.js
4-
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
5-
* MIT Licensed
6-
*/
3+
* Log.js
4+
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
5+
* MIT Licensed
6+
*/
7+
8+
/**
9+
* Module dependencies.
10+
*/
11+
12+
var EventEmitter = require('events').EventEmitter;
713

814
/**
915
* Initialize a `Loggeer` with the given log `level` defaulting
@@ -18,6 +24,7 @@ var Log = exports = module.exports = function Log(level, stream){
1824
if ('string' == typeof level) level = exports[level.toUpperCase()];
1925
this.level = level || exports.DEBUG;
2026
this.stream = stream || process.stdout;
27+
if (stream.readable) this.read();
2128
};
2229

2330
/**
@@ -89,7 +96,35 @@ exports.DEBUG = 7;
8996
*/
9097

9198
Log.prototype = {
92-
99+
100+
/**
101+
* Start emitting "line" events.
102+
*
103+
* @api public
104+
*/
105+
106+
read: function(){
107+
var buf = ''
108+
, self = this;
109+
this.stream.setEncoding('ascii');
110+
this.stream.on('data', function(chunk){
111+
buf += chunk;
112+
if ('\n' != buf[buf.length - 1]) return;
113+
buf.split('\n').map(function(line){
114+
if (!line.length) return;
115+
var captures = line.match(/^\[([^\]]+)\] (\w+) (.*)/);
116+
var obj = {
117+
date: new Date(captures[1])
118+
, level: exports[captures[2]]
119+
, levelString: captures[2]
120+
, msg: captures[3]
121+
};
122+
self.emit('line', obj);
123+
});
124+
buf = '';
125+
});
126+
},
127+
93128
/**
94129
* Log output message.
95130
*
@@ -196,4 +231,10 @@ Log.prototype = {
196231
debug: function(msg){
197232
this.log('DEBUG', msg);
198233
}
199-
};
234+
};
235+
236+
/**
237+
* Inherit from `EventEmitter`.
238+
*/
239+
240+
Log.prototype.__proto__ = EventEmitter.prototype;

0 commit comments

Comments
 (0)