File tree Expand file tree Collapse file tree 3 files changed +62
-13
lines changed Expand file tree Collapse file tree 3 files changed +62
-13
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change
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
+ } ) ; ;
Original file line number Diff line number Diff line change 1
1
2
2
/*!
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 ;
7
13
8
14
/**
9
15
* Initialize a `Loggeer` with the given log `level` defaulting
@@ -18,8 +24,15 @@ var Log = exports = module.exports = function Log(level, stream){
18
24
if ( 'string' == typeof level ) level = exports [ level . toUpperCase ( ) ] ;
19
25
this . level = level || exports . DEBUG ;
20
26
this . stream = stream || process . stdout ;
27
+ if ( stream . readable ) this . read ( ) ;
21
28
} ;
22
29
30
+ /**
31
+ * Inherit from `EventEmitter`.
32
+ */
33
+
34
+ Log . prototype . __proto__ = EventEmitter . prototype ;
35
+
23
36
/**
24
37
* System is unusable.
25
38
*
@@ -89,7 +102,30 @@ exports.DEBUG = 7;
89
102
*/
90
103
91
104
Log . prototype = {
92
-
105
+
106
+ /**
107
+ * Start emitting "line" events.
108
+ *
109
+ * @api public
110
+ */
111
+
112
+ read : function ( ) {
113
+ var self = this ;
114
+ this . stream . setEncoding ( 'ascii' ) ;
115
+ this . stream . on ( 'data' , function ( chunk ) {
116
+ chunk . split ( '\n' ) . map ( function ( line ) {
117
+ var captures = line . match ( / ^ \[ ( [ ^ \] ] + ) \] ( \w + ) ( .* ) / ) ;
118
+ var obj = {
119
+ date : new Date ( captures [ 1 ] )
120
+ , level : exports [ captures [ 2 ] ]
121
+ , levelString : captures [ 2 ]
122
+ , msg : captures [ 3 ]
123
+ } ;
124
+ self . emit ( 'line' , line ) ;
125
+ } ) ;
126
+ } ) ;
127
+ } ,
128
+
93
129
/**
94
130
* Log output message.
95
131
*
You can’t perform that action at this time.
0 commit comments