Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add more examples: file, stream

  • Loading branch information...
commit 0cc9543c4bbef174dd6bbf14a9c0db8ffcf7d9ee 1 parent fa2e09f
LI Long authored March 05, 2012
132  README.md
Source Rendered
@@ -40,11 +40,17 @@ var logger = require('tracer').colorConsole({level:2});
40 40
 Simple Example
41 41
 --------------
42 42
 
  43
+some helper package is need, so install -dev for running examples
  44
+
  45
+```javascript
  46
+npm install -dev tracer
  47
+```
  48
+
43 49
 ### Simple Console
44 50
 
45 51
 ```javascript
46 52
 var logger = require('tracer').console();
47  
-logger.test('hello');
  53
+logger.log('hello');
48 54
 logger.trace('hello', 'world');
49 55
 logger.debug('hello %s',  'world', 123);
50 56
 logger.info('hello %s %d',  'world', 123, {foo:'bar'});
@@ -52,7 +58,7 @@ logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
52 58
 logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
53 59
 
54 60
 $ node example/console.js 
55  
-2012-03-02T13:35:22.83Z <test> console.js:3 (Object.<anonymous>) hello
  61
+2012-03-02T13:35:22.83Z <log> console.js:3 (Object.<anonymous>) hello
56 62
 2012-03-02T13:35:22.85Z <trace> console.js:4 (Object.<anonymous>) hello world
57 63
 2012-03-02T13:35:22.85Z <debug> console.js:5 (Object.<anonymous>) hello world 123
58 64
 2012-03-02T13:35:22.85Z <info> console.js:6 (Object.<anonymous>) hello world 123 { foo: 'bar' }
@@ -63,7 +69,7 @@ $ node example/console.js
63 69
 ### Color Console
64 70
 ```javascript
65 71
 var logger = require('tracer').colorConsole();
66  
-logger.test('hello');
  72
+logger.log('hello');
67 73
 logger.trace('hello', 'world');
68 74
 logger.debug('hello %s',  'world', 123);
69 75
 logger.info('hello %s %d',  'world', 123, {foo:'bar'});
@@ -77,9 +83,23 @@ Advanced Example
77 83
 Take a look at the examples directory for different examples.
78 84
 
79 85
 ### Set logging level
  86
+
  87
+the level option support index (number) or method name.
  88
+
  89
+
  90
+```javascript
  91
+var logger = require('tracer').console({level:'warn'});
  92
+```
  93
+equal
  94
+
  95
+```javascript
  96
+var logger = require('tracer').console({level:4});
  97
+```
  98
+
  99
+
80 100
 ```javascript
81  
-var logger = require('tracer').console({level:2});
82  
-logger.test('hello');
  101
+var logger = require('tracer').console({level:'warn'});
  102
+logger.log('hello');
83 103
 logger.trace('hello', 'world');
84 104
 logger.debug('hello %s',  'world', 123);
85 105
 logger.info('hello %s %d',  'world', 123, {foo:'bar'});
@@ -87,8 +107,6 @@ logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
87 107
 logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
88 108
 
89 109
 //$ node example/level.js 
90  
-//2012-03-02T13:41:33.17Z <debug> level.js:4 (Object.<anonymous>) hello world 123
91  
-//2012-03-02T13:41:33.28Z <info> level.js:5 (Object.<anonymous>) hello world 123 { foo: 'bar' }
92 110
 //2012-03-02T13:41:33.29Z <warn> level.js:6 (Object.<anonymous>) hello world 123 {"foo":"bar"}
93 111
 //2012-03-02T13:41:33.30Z <error> level.js:7 (Object.<anonymous>) hello world 123 {"foo":"bar"} [ 1, 2, 3, 4 ] function Object() { [native code] }
94 112
 
@@ -102,7 +120,7 @@ logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
102 120
 format tag:   
103 121
 
104 122
 *  timestamp: current time    
105  
-*  title: method name, default is 'test', 'trace', 'debug', 'info', 'warn', 'error'   
  123
+*  title: method name, default is 'log', 'trace', 'debug', 'info', 'warn', 'error'   
106 124
 *  message: printf message, support %s string, %d number, %j JSON and auto inspect   
107 125
 *  file: file name   
108 126
 *  line: line number   
@@ -127,7 +145,7 @@ var logger = require('tracer').console(
127 145
 
128 146
 ### Customize methods and filters 
129 147
 
130  
-each filtes function was called. the function must be like
  148
+each filtes function was called. the function is synchronous and must be like
131 149
 
132 150
 ```javascript
133 151
 function f1(str) {
@@ -135,6 +153,22 @@ function f1(str) {
135 153
 }
136 154
 ```
137 155
 
  156
+or you can use the second parameter
  157
+
  158
+```javascript
  159
+function f1(str, data) {
  160
+	if( data.title === 'log5' ){
  161
+		// data.title === 'error' or data.title === 'warn'
  162
+		//do some thing, example write to database, you can use async write to do this
  163
+		
  164
+		//if you don't want contine other filter, then 
  165
+		//return false; 
  166
+	}
  167
+	return str.toUpperCase();
  168
+}
  169
+```
  170
+
  171
+
138 172
 About [Colors.js](https://github.com/Marak/colors.js)
139 173
 
140 174
 ```javascript
@@ -159,26 +193,82 @@ logger.log5('hello %s %d', 'world', 123);
159 193
 
160 194
 ```
161 195
 
  196
+### Log File Transport
  197
+```javascript
  198
+var fs = require('fs');
  199
+
  200
+var logger = require('tracer').console({
  201
+	transpot : function(data) {
  202
+		console.log(data.output);
  203
+		fs.open('./file.log', 'a', 0666, function(e, id) {
  204
+			fs.write(id, data.output+"\n", null, 'utf8', function() {
  205
+				fs.close(id, function() {
  206
+				});
  207
+			});
  208
+		});
  209
+	}
  210
+});
  211
+
  212
+logger.log('hello');
  213
+logger.trace('hello', 'world');
  214
+logger.debug('hello %s', 'world', 123);
  215
+logger.info('hello %s %d', 'world', 123, {foo : 'bar'});
  216
+logger.warn('hello %s %d %j', 'world', 123, {foo : 'bar'});
  217
+logger.error('hello %s %d %j', 'world', 123, {foo : 'bar'}, [ 1, 2, 3, 4 ], Object);
  218
+
  219
+```
  220
+
  221
+### Stream Transport
  222
+```javascript
  223
+var fs = require('fs');
  224
+
  225
+var logger = require('tracer').console({
  226
+		transpot : function(data) {
  227
+			console.log(data.output);
  228
+			var stream = fs.createWriteStream("./stream.log", {
  229
+			    flags: "a",
  230
+			    encoding: "utf8",
  231
+			    mode: 0666
  232
+			}).write(data.output+"\n");
  233
+		}
  234
+});
  235
+
  236
+logger.log('hello');
  237
+logger.trace('hello', 'world');
  238
+logger.debug('hello %s',  'world', 123);
  239
+logger.info('hello %s %d',  'world', 123, {foo:'bar'});
  240
+logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
  241
+logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
162 242
 
  243
+```
163 244
 
164 245
 ### MongoDB Transport
165 246
 ```javascript
166 247
 var mongo = require('mongoskin');
167 248
 var db = mongo.db("127.0.0.1:27017/test?auto_reconnect");
168 249
 
169  
-//var logger = require('tracer').console();
  250
+var log_conf = {
  251
+		transpot : function(data) {
  252
+			console.log(data.output);
  253
+			var loginfo = db.collection("loginfo");
  254
+			loginfo.insert( data, function(err, log) {
  255
+				if (err) {
  256
+					console.error(err);
  257
+				}
  258
+			});
  259
+		}
  260
+}
170 261
 
171  
-var dblogger = require('tracer').console({
172  
-	transpot : function(data) {
173  
-		//logger.info(data.output);
174  
-		var loginfo = db.collection("loginfo");
175  
-		loginfo.insert( data, function(err, log) {
176  
-			if (err) {
177  
-				//logger.error(err);
178  
-			}
179  
-		});
180  
-	},
181  
-});
  262
+var logger = require('tracer').console(log_conf);
  263
+
  264
+logger.log('hello');
  265
+logger.trace('hello', 'world');
  266
+logger.debug('hello %s',  'world', 123);
  267
+logger.info('hello %s %d',  'world', 123, {foo:'bar'});
  268
+logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
  269
+logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
  270
+
  271
+console.log('\n\n\npress ctrl-c to exit');
182 272
 
183 273
 ```
184 274
 
4  example/colorConsole.js
... ...
@@ -1,6 +1,6 @@
1 1
 
2 2
 var logger = require('tracer').colorConsole();
3  
-logger.test('hello');
  3
+logger.log('hello');
4 4
 logger.trace('hello', 'world');
5 5
 logger.debug('hello %s',  'world', 123);
6 6
 logger.info('hello %s %d',  'world', 123, {foo:'bar'});
@@ -8,7 +8,7 @@ logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
8 8
 logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
9 9
 
10 10
 //$ node example/colorConsole.js 
11  
-//2012-03-02T13:43:09.27Z <test> colorConsole.js:3 (Object.<anonymous>) hello
  11
+//2012-03-02T13:43:09.27Z <log> colorConsole.js:3 (Object.<anonymous>) hello
12 12
 //2012-03-02T13:43:09.29Z <trace> colorConsole.js:4 (Object.<anonymous>) hello world
13 13
 //2012-03-02T13:43:09.29Z <debug> colorConsole.js:5 (Object.<anonymous>) hello world 123
14 14
 //2012-03-02T13:43:09.29Z <info> colorConsole.js:6 (Object.<anonymous>) hello world 123 { foo: 'bar' }
4  example/console.js
... ...
@@ -1,6 +1,6 @@
1 1
 
2 2
 var logger = require('tracer').console();
3  
-logger.test('hello');
  3
+logger.log('hello');
4 4
 logger.trace('hello', 'world');
5 5
 logger.debug('hello %s',  'world', 123);
6 6
 logger.info('hello %s %d',  'world', 123, {foo:'bar'});
@@ -9,7 +9,7 @@ logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
9 9
 
10 10
 
11 11
 //$ node example/console.js 
12  
-//2012-03-02T13:42:00.99Z <test> console.js:3 (Object.<anonymous>) hello
  12
+//2012-03-02T13:42:00.99Z <log> console.js:3 (Object.<anonymous>) hello
13 13
 //2012-03-02T13:42:01.02Z <trace> console.js:4 (Object.<anonymous>) hello world
14 14
 //2012-03-02T13:42:01.03Z <debug> console.js:5 (Object.<anonymous>) hello world 123
15 15
 //2012-03-02T13:42:01.05Z <info> console.js:6 (Object.<anonymous>) hello world 123 { foo: 'bar' }
20  example/file.js
... ...
@@ -0,0 +1,20 @@
  1
+var fs = require('fs');
  2
+
  3
+var logger = require('tracer').console({
  4
+	transpot : function(data) {
  5
+		console.log(data.output);
  6
+		fs.open('./file.log', 'a', 0666, function(e, id) {
  7
+			fs.write(id, data.output+"\n", null, 'utf8', function() {
  8
+				fs.close(id, function() {
  9
+				});
  10
+			});
  11
+		});
  12
+	}
  13
+});
  14
+
  15
+logger.log('hello');
  16
+logger.trace('hello', 'world');
  17
+logger.debug('hello %s', 'world', 123);
  18
+logger.info('hello %s %d', 'world', 123, {foo : 'bar'});
  19
+logger.warn('hello %s %d %j', 'world', 123, {foo : 'bar'});
  20
+logger.error('hello %s %d %j', 'world', 123, {foo : 'bar'}, [ 1, 2, 3, 4 ], Object);
16  example/format.js
@@ -5,22 +5,16 @@ var logger = require('tracer')
5 5
 					dateformat : "HH:MM:ss.L"
6 6
 				});
7 7
 
8  
-logger.test('hello');
  8
+logger.log('hello');
9 9
 logger.trace('hello', 'world');
10 10
 logger.debug('hello %s', 'world', 123);
11  
-logger.info('hello %s %d', 'world', 123, {
12  
-	foo : 'bar'
13  
-});
14  
-logger.warn('hello %s %d %j', 'world', 123, {
15  
-	foo : 'bar'
16  
-});
17  
-logger.error('hello %s %d %j', 'world', 123, {
18  
-	foo : 'bar'
19  
-}, [ 1, 2, 3, 4 ], Object);
  11
+logger.info('hello %s %d', 'world', 123, {foo : 'bar'});
  12
+logger.warn('hello %s %d %j', 'world', 123, {foo : 'bar'});
  13
+logger.error('hello %s %d %j', 'world', 123, {foo : 'bar'}, [ 1, 2, 3, 4 ], Object);
20 14
 
21 15
 
22 16
 //$ node example/format.js 
23  
-//13:52:30.61 <test> hello (in format.js:7)
  17
+//13:52:30.61 <log> hello (in format.js:7)
24 18
 //13:52:30.63 <trace> hello world (in format.js:8)
25 19
 //13:52:30.63 <debug> hello world 123 (in format.js:9)
26 20
 //13:52:30.63 <info> hello world 123 { foo: 'bar' } (in format.js:10)
6  example/level.js
... ...
@@ -1,5 +1,5 @@
1  
-var logger = require('tracer').console({level:2});
2  
-logger.test('hello');
  1
+var logger = require('tracer').console({level:'warn'});
  2
+logger.log('hello');
3 3
 logger.trace('hello', 'world');
4 4
 logger.debug('hello %s',  'world', 123);
5 5
 logger.info('hello %s %d',  'world', 123, {foo:'bar'});
@@ -7,7 +7,5 @@ logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
7 7
 logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
8 8
 
9 9
 //$ node example/level.js 
10  
-//2012-03-02T13:41:33.17Z <debug> level.js:4 (Object.<anonymous>) hello world 123
11  
-//2012-03-02T13:41:33.28Z <info> level.js:5 (Object.<anonymous>) hello world 123 { foo: 'bar' }
12 10
 //2012-03-02T13:41:33.29Z <warn> level.js:6 (Object.<anonymous>) hello world 123 {"foo":"bar"}
13 11
 //2012-03-02T13:41:33.30Z <error> level.js:7 (Object.<anonymous>) hello world 123 {"foo":"bar"} [ 1, 2, 3, 4 ] function Object() { [native code] }
10  example/methods.js
... ...
@@ -1,13 +1,19 @@
1 1
 var colors = require('colors');
2 2
 
3  
-function f1(str) {
  3
+function f1(str, data) {
  4
+	if( data.title === 'log5' ){
  5
+		//do some thing, example write to database
  6
+		
  7
+		//if you don't want contine other filter, then 
  8
+		return false; 
  9
+	}
4 10
 	return str.toUpperCase();
5 11
 }
6 12
 
7 13
 var logger = require('tracer').colorConsole({
8 14
 	level : 1,
9 15
 	methods : [ 'log0', 'log1', 'log2', 'log3', 'log4', 'log5' ],
10  
-	filters : [f1, colors.bold, colors.italic, colors.underline, colors.inverse, colors.yellow],
  16
+	filters : [f1, colors.underline, colors.yellow],
11 17
 
12 18
 });
13 19
 logger.log0('hello');
36  example/mongodb.js
... ...
@@ -1,25 +1,25 @@
1 1
 var mongo = require('mongoskin');
2 2
 var db = mongo.db("127.0.0.1:27017/test?auto_reconnect");
3 3
 
4  
-//var logger = require('tracer').console();
  4
+var log_conf = {
  5
+		transpot : function(data) {
  6
+			console.log(data.output);
  7
+			var loginfo = db.collection("loginfo");
  8
+			loginfo.insert( data, function(err, log) {
  9
+				if (err) {
  10
+					console.error(err);
  11
+				}
  12
+			});
  13
+		}
  14
+}
5 15
 
6  
-var dblogger = require('tracer').console({
7  
-	transpot : function(data) {
8  
-		//logger.info(data.output);
9  
-		var loginfo = db.collection("loginfo");
10  
-		loginfo.insert( data, function(err, log) {
11  
-			if (err) {
12  
-				//logger.error(err);
13  
-			}
14  
-		});
15  
-	},
16  
-});
  16
+var logger = require('tracer').console(log_conf);
17 17
 
18  
-dblogger.test('hello');
19  
-dblogger.trace('hello', 'world');
20  
-dblogger.debug('hello %s',  'world', 123);
21  
-dblogger.info('hello %s %d',  'world', 123, {foo:'bar'});
22  
-dblogger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
23  
-dblogger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
  18
+logger.log('hello');
  19
+logger.trace('hello', 'world');
  20
+logger.debug('hello %s',  'world', 123);
  21
+logger.info('hello %s %d',  'world', 123, {foo:'bar'});
  22
+logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
  23
+logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
24 24
 
25 25
 console.log('\n\n\npress ctrl-c to exit');
19  example/stream.js
... ...
@@ -0,0 +1,19 @@
  1
+var fs = require('fs');
  2
+
  3
+var logger = require('tracer').console({
  4
+		transpot : function(data) {
  5
+			console.log(data.output);
  6
+			var stream = fs.createWriteStream("./stream.log", {
  7
+			    flags: "a",
  8
+			    encoding: "utf8",
  9
+			    mode: 0666
  10
+			}).write(data.output+"\n");
  11
+		}
  12
+});
  13
+
  14
+logger.log('hello');
  15
+logger.trace('hello', 'world');
  16
+logger.debug('hello %s',  'world', 123);
  17
+logger.info('hello %s %d',  'world', 123, {foo:'bar'});
  18
+logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
  19
+logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
3  lib/color_console.js
@@ -2,7 +2,8 @@ var colors = require('colors');
2 2
 module.exports = function(conf) {
3 3
 	return require('./console')({
4 4
 		filters : {
5  
-			test : colors.magenta,
  5
+			//log : colors.black,
  6
+			trace : colors.magenta,
6 7
 			debug : colors.blue,
7 8
 			info : colors.green,
8 9
 			warn : colors.yellow,
27  lib/console.js
@@ -8,8 +8,8 @@ module.exports = (function() {
8 8
 			console.log(data.output);
9 9
 		},
10 10
 		filters : [],
11  
-		level : 0,
12  
-		methods : [ 'test', 'trace', 'debug', 'info', 'warn', 'error' ]
  11
+		level : 'log',
  12
+		methods : [ 'log', 'trace', 'debug', 'info', 'warn', 'error' ]
13 13
 	};
14 14
 
15 15
 	var _union = function(obj, args) {
@@ -25,9 +25,7 @@ module.exports = (function() {
25 25
 	
26 26
 	_config = _union(_config, arguments);
27 27
 
28  
-	var _log = function(level, title, args) {
29  
-		if (level < _config.level)
30  
-			return;
  28
+	var _log = function(title, args) {
31 29
 		var msg = msgformat.apply(this, args);
32 30
 		var data = {
33 31
 			timestamp : dateFormat(new Date(), _config.dateformat),
@@ -60,7 +58,7 @@ module.exports = (function() {
60 58
 				filters = Array.isArray(filters) ? filters : [ filters ];
61 59
 				var i, len = filters.length;
62 60
 				for (i = 0; i < len; i += 1) {
63  
-					data.output = filters[i](data.output);
  61
+					data.output = filters[i](data.output, data);
64 62
 					if (!data.output)
65 63
 						return data;
66 64
 				}
@@ -72,14 +70,19 @@ module.exports = (function() {
72 70
 
73 71
 	var _self = {};
74 72
 
75  
-	var len = _config.methods.length
  73
+	var len = _config.methods.length;
  74
+	if(typeof(_config.level)=='string')
  75
+		_config.level=_config.methods.indexOf(_config.level);
76 76
 	for ( var i = 0; i < len; ++i) {
77 77
 		var method = _config.methods[i];
78  
-		_self[method] = (function(level, title) {
79  
-			return (function() {
80  
-				return _log(level, title, arguments);
81  
-			});
82  
-		})(i, method);
  78
+		if (i < _config.level)
  79
+			_self[method] = (function(){});
  80
+		else
  81
+			_self[method] = (function(title) {
  82
+				return (function() {
  83
+					return _log(title, arguments);
  84
+				});
  85
+			})(method);
83 86
 	}
84 87
 
85 88
 	return _self;
4  package.json
... ...
@@ -1,8 +1,8 @@
1 1
 {
2 2
   "name": "tracer",
3  
-  "description": "A powerful and customizable logging library for node.js. keywords: log, logger, trace ",
  3
+  "description": "A powerful and customizable logging library for node.js. support color console with timestamp, line number, method name, file name and call stack. you can set transport to file, stream, database(ex: mongodb and clouddb, simpledb). keywords: log, logger, trace ",
4 4
   "homepage": "http://github.com/baryon/tracer",
5  
-  "version": "0.1.0",
  5
+  "version": "0.2.0",
6 6
   "author": "LI Long <lilong@gmail.com>",
7 7
   "dependencies": {
8 8
     "dateformat": "1.0.2-1.2.3",
6  test/test.js
@@ -8,7 +8,7 @@ exports["simple message"] = function() {
8 8
 			return data;
9 9
 		}
10 10
 	});
11  
-	var o = logger.test('hello');
  11
+	var o = logger.log('hello');
12 12
 	assert.equal(o['output'], 'hello');
13 13
 }
14 14
 
@@ -20,6 +20,6 @@ exports["simple console message"] = function() {
20 20
 			return data;
21 21
 		}
22 22
 	});
23  
-	var o = logger.test('hello');
24  
-	assert.equal(o.output, '\u001b[35mhello\u001b[39m');
  23
+	var o = logger.debug('hello');
  24
+	assert.equal(o.output, '\u001b[34mhello\u001b[39m');
25 25
 }

0 notes on commit 0cc9543

Please sign in to comment.
Something went wrong with that request. Please try again.