Skip to content
Newer
Older
100644 466 lines (339 sloc) 11.8 KB
1c1e99f @baryon add trsvis-ci build stat
authored Mar 5, 2012
1 #tracer for node.js [![Build Status](https://secure.travis-ci.org/baryon/tracer.png)](http://travis-ci.org/baryon/tracer)
2cbbaca @baryon npm public
authored Mar 2, 2012
2
3 A powerful and customizable logging library for node.js.
4
5 ===========
6 ##Features
7 -----
8
9 * print log messages with timstamp, file name, method name, line number, path or call stack
10 * be customized output format with micro-template and timestamp format
11 * support user-defined logging levels
12 * add easily any transport
13 * support filter functions, so print statements in full color and font (color console)
14
15 ##Install
16 -----
17 ```javascript
18 npm install tracer
19 ```
20
21 Usage
22 -----
23 Add to your code:
24
bdf3896 @baryon fixed some words
authored Mar 5, 2012
25 Simple Console
074e84b @baryon fixed some words
authored Mar 5, 2012
26
2cbbaca @baryon npm public
authored Mar 2, 2012
27 ```javascript
28 var logger = require('tracer').console();
29 ```
30
31
bdf3896 @baryon fixed some words
authored Mar 5, 2012
32 Color Console
074e84b @baryon fixed some words
authored Mar 5, 2012
33
2cbbaca @baryon npm public
authored Mar 2, 2012
34 ```javascript
35 var logger = require('tracer').colorConsole();
36 ```
37
bdf3896 @baryon fixed some words
authored Mar 5, 2012
38 Set Output Level
074e84b @baryon fixed some words
authored Mar 5, 2012
39
2cbbaca @baryon npm public
authored Mar 2, 2012
40 ```javascript
bdf3896 @baryon fixed some words
authored Mar 5, 2012
41 var logger = require('tracer').colorConsole({level:'warn'});
2cbbaca @baryon npm public
authored Mar 2, 2012
42 ```
43
44
45 Simple Example
46 --------------
47
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
48
49
08a5077 @baryon fix format
authored Mar 2, 2012
50 ### Simple Console
51
2cbbaca @baryon npm public
authored Mar 2, 2012
52 ```javascript
53 var logger = require('tracer').console();
564cd2d @baryon updated to 0.4.0, add dailylog
authored Mar 21, 2012
54
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
55 logger.log('hello');
2cbbaca @baryon npm public
authored Mar 2, 2012
56 logger.trace('hello', 'world');
57 logger.debug('hello %s', 'world', 123);
58 logger.info('hello %s %d', 'world', 123, {foo:'bar'});
59 logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
60 logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
61
62 $ node example/console.js
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
63 2012-03-02T13:35:22.83Z <log> console.js:3 (Object.<anonymous>) hello
2cbbaca @baryon npm public
authored Mar 2, 2012
64 2012-03-02T13:35:22.85Z <trace> console.js:4 (Object.<anonymous>) hello world
65 2012-03-02T13:35:22.85Z <debug> console.js:5 (Object.<anonymous>) hello world 123
66 2012-03-02T13:35:22.85Z <info> console.js:6 (Object.<anonymous>) hello world 123 { foo: 'bar' }
67 2012-03-02T13:35:22.85Z <warn> console.js:7 (Object.<anonymous>) hello world 123 {"foo":"bar"}
68 2012-03-02T13:35:22.85Z <error> console.js:8 (Object.<anonymous>) hello world 123 {"foo":"bar"} [ 1, 2, 3, 4 ] function Object() { [native code] }
69 ```
70
08a5077 @baryon fix format
authored Mar 2, 2012
71 ### Color Console
72 ```javascript
73 var logger = require('tracer').colorConsole();
564cd2d @baryon updated to 0.4.0, add dailylog
authored Mar 21, 2012
74
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
75 logger.log('hello');
08a5077 @baryon fix format
authored Mar 2, 2012
76 logger.trace('hello', 'world');
77 logger.debug('hello %s', 'world', 123);
78 logger.info('hello %s %d', 'world', 123, {foo:'bar'});
79 logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
80 logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
81 ```
2cbbaca @baryon npm public
authored Mar 2, 2012
82
564cd2d @baryon updated to 0.4.0, add dailylog
authored Mar 21, 2012
83 ### Daily Log
84 ```javascript
85 var logger = require('tracer').dailyfile({root:'.'});
86
87 logger.log('hello');
88 logger.trace('hello', 'world');
89 logger.debug('hello %s', 'world', 123);
90 logger.info('hello %s %d', 'world', 123, {foo:'bar'});
91 logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
92 logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
93 ```
94
95 dailylog will output all types log to diff files every day like log4j
96
2cbbaca @baryon npm public
authored Mar 2, 2012
97 Advanced Example
98 ---------------
bdf3896 @baryon fixed some words
authored Mar 5, 2012
99 some helper package is need, so install -dev for running examples
100
101 ```javascript
102 npm install -dev tracer
103 ```
104
2cbbaca @baryon npm public
authored Mar 2, 2012
105
106 Take a look at the examples directory for different examples.
107
108 ### Set logging level
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
109
110 the level option support index (number) or method name.
111
112
113 ```javascript
114 var logger = require('tracer').console({level:'warn'});
115 ```
116 equal
117
118 ```javascript
119 var logger = require('tracer').console({level:4});
120 ```
121
122
2cbbaca @baryon npm public
authored Mar 2, 2012
123 ```javascript
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
124 var logger = require('tracer').console({level:'warn'});
125 logger.log('hello');
2cbbaca @baryon npm public
authored Mar 2, 2012
126 logger.trace('hello', 'world');
127 logger.debug('hello %s', 'world', 123);
128 logger.info('hello %s %d', 'world', 123, {foo:'bar'});
129 logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
130 logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
131
132 //$ node example/level.js
133 //2012-03-02T13:41:33.29Z <warn> level.js:6 (Object.<anonymous>) hello world 123 {"foo":"bar"}
134 //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] }
135
b04fc40 @baryon updated to 0.3.0
authored Mar 7, 2012
136 //log,trace, debug and info level was not ouputed
2cbbaca @baryon npm public
authored Mar 2, 2012
137
32665a3 @baryon fix format
authored Mar 2, 2012
138 ```
2cbbaca @baryon npm public
authored Mar 2, 2012
139
3feac15 @baryon fix format
authored Mar 2, 2012
140
141
2cbbaca @baryon npm public
authored Mar 2, 2012
142 ### Customize output format
f6b8f02 @baryon fixed format
authored Mar 2, 2012
143 format tag:
fa2e09f @baryon fixed format
authored Mar 2, 2012
144
f6b8f02 @baryon fixed format
authored Mar 2, 2012
145 * timestamp: current time
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
146 * title: method name, default is 'log', 'trace', 'debug', 'info', 'warn', 'error'
f773aea @baryon updated to 0.3.4, use tinytim package
authored Mar 13, 2012
147 * level: method level, default is 'log':0, 'trace':1, 'debug':2, 'info':3, 'warn':4, 'error':5
f6b8f02 @baryon fixed format
authored Mar 2, 2012
148 * message: printf message, support %s string, %d number, %j JSON and auto inspect
149 * file: file name
150 * line: line number
151 * pos: postion
152 * path: file's path
153 * method: method name of caller
4e649e0 @baryon update to 0.2.1
authored Mar 5, 2012
154 * stack: call stack
f6b8f02 @baryon fixed format
authored Mar 2, 2012
155
564cd2d @baryon updated to 0.4.0, add dailylog
authored Mar 21, 2012
156 we use tinytim micro-template system to output log. see details [tinytim](https://github.com/baryon/node-tinytim).
157 and, we use [Date Format](http://blog.stevenlevithan.com/archives/date-time-format) to format datetime.
158
159
2cbbaca @baryon npm public
authored Mar 2, 2012
160
161 ```javascript
162 var logger = require('tracer').console(
163 {
164 format : "{{timestamp}} <{{title}}> {{message}} (in {{file}}:{{line}})",
165 dateformat : "HH:MM:ss.L"
166 });
167
32665a3 @baryon fix format
authored Mar 2, 2012
168 ```
2cbbaca @baryon npm public
authored Mar 2, 2012
169
3feac15 @baryon fix format
authored Mar 2, 2012
170
b04fc40 @baryon updated to 0.3.0
authored Mar 7, 2012
171 Or, you can set special format for output method
3feac15 @baryon fix format
authored Mar 2, 2012
172
b04fc40 @baryon updated to 0.3.0
authored Mar 7, 2012
173 ```javascript
174 var logger = require('tracer')
175 .colorConsole(
176 {
177 format : [
178 "{{timestamp}} <{{title}}> {{message}} (in {{file}}:{{line}})", //default format
179 {
564cd2d @baryon updated to 0.4.0, add dailylog
authored Mar 21, 2012
180 error : "{{timestamp}} <{{title}}> {{message}} (in {{file}}:{{line}})\nCall Stack:{{stacklist}}" // error format
b04fc40 @baryon updated to 0.3.0
authored Mar 7, 2012
181 }
182 ],
564cd2d @baryon updated to 0.4.0, add dailylog
authored Mar 21, 2012
183 dateformat : "HH:MM:ss.L",
184 preprocess : function(data){
185 if(data.title==='error'){
186 var callstack = '',len=data.stack.length;
187 for(var i=0; i<len; i+=1){
188 callstack += '\n'+data.stack[i];
189 }
190 data.stacklist = callstack;
191 }
192
193 data.title = data.title.toUpperCase();
194 }
b04fc40 @baryon updated to 0.3.0
authored Mar 7, 2012
195 });
196 ```
197
564cd2d @baryon updated to 0.4.0, add dailylog
authored Mar 21, 2012
198 the preprocess method is a choice for changing tag.
199
200
201
b04fc40 @baryon updated to 0.3.0
authored Mar 7, 2012
202 ### Customize output methods
203
204 ```javascript
205 var colors = require('colors');
206
207 var logger = require('tracer').colorConsole({
208 level : 'log1',
209 methods : [ 'log0', 'log1', 'log2', 'log3', 'log4', 'log5' ],
210 filters : [colors.underline, colors.yellow],
211
212 });
213 logger.log0('hello');
214 logger.log1('hello', 'world');
215 logger.log2('hello %s', 'world', 123);
216 logger.log4('hello %s %d', 'world', 123);
217 logger.log5('hello %s %d', 'world', 123);
218 ```
219
220
221
222 ### Customize filters
2cbbaca @baryon npm public
authored Mar 2, 2012
223
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
224 each filtes function was called. the function is synchronous and must be like
2cbbaca @baryon npm public
authored Mar 2, 2012
225
226 ```javascript
227 function f1(str) {
228 return str.toUpperCase();
229 }
32665a3 @baryon fix format
authored Mar 2, 2012
230 ```
2cbbaca @baryon npm public
authored Mar 2, 2012
231
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
232 or you can use the second parameter
233
234 ```javascript
235 function f1(str, data) {
b04fc40 @baryon updated to 0.3.0
authored Mar 7, 2012
236 if( data.title === 'error' ){
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
237 //do some thing, example write to database, you can use async write to do this
238
eaa236e @baryon fix minor message
authored Mar 5, 2012
239 //if you don't want continue other filter, then
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
240 //return false;
241 }
242 return str.toUpperCase();
243 }
244 ```
245
246
2cbbaca @baryon npm public
authored Mar 2, 2012
247 About [Colors.js](https://github.com/Marak/colors.js)
248
249 ```javascript
250 var colors = require('colors');
251 var logger = require('tracer').colorConsole({
b04fc40 @baryon updated to 0.3.0
authored Mar 7, 2012
252 filters : [
253 f1, colors.underline, colors.blue, //default filter
254 //the last item can be custom filter. here is "warn" and "error" filter
255 {
256 warn : colors.yellow,
257 error : [f1, colors.red, colors.bold ]
258 }
259 ]
2cbbaca @baryon npm public
authored Mar 2, 2012
260 });
261
32665a3 @baryon fix format
authored Mar 2, 2012
262 ```
2cbbaca @baryon npm public
authored Mar 2, 2012
263
b04fc40 @baryon updated to 0.3.0
authored Mar 7, 2012
264 the filter support key-function pair, example: [color_console.js](https://github.com/baryon/tracer/blob/master/lib/color_console.js)
00768a5 @baryon fixed some words
authored Mar 5, 2012
265
266 ```javascript
267 {
268 filters : {
269 //log : colors.black,
270 trace : colors.magenta,
271 debug : colors.blue,
272 info : colors.green,
273 warn : colors.yellow,
274 error : [ colors.red, colors.bold ]
275 }
276 }
277 ```
278
b04fc40 @baryon updated to 0.3.0
authored Mar 7, 2012
279 and the filters is an array, the last item can be custom filter. see example:[filter.js](https://github.com/baryon/tracer/blob/master/example/filter.js)
00768a5 @baryon fixed some words
authored Mar 5, 2012
280
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
281 ### Log File Transport
282 ```javascript
283 var fs = require('fs');
284
285 var logger = require('tracer').console({
8b3dfdb @baryon updated to 0.3.3
authored Mar 9, 2012
286 transport : function(data) {
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
287 console.log(data.output);
288 fs.open('./file.log', 'a', 0666, function(e, id) {
289 fs.write(id, data.output+"\n", null, 'utf8', function() {
290 fs.close(id, function() {
291 });
292 });
293 });
294 }
295 });
296
297 logger.log('hello');
298 logger.trace('hello', 'world');
299 logger.debug('hello %s', 'world', 123);
300 logger.info('hello %s %d', 'world', 123, {foo : 'bar'});
301 logger.warn('hello %s %d %j', 'world', 123, {foo : 'bar'});
302 logger.error('hello %s %d %j', 'world', 123, {foo : 'bar'}, [ 1, 2, 3, 4 ], Object);
303
304 ```
305
306 ### Stream Transport
307 ```javascript
308 var fs = require('fs');
309
310 var logger = require('tracer').console({
8b3dfdb @baryon updated to 0.3.3
authored Mar 9, 2012
311 transport : function(data) {
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
312 console.log(data.output);
313 var stream = fs.createWriteStream("./stream.log", {
314 flags: "a",
315 encoding: "utf8",
316 mode: 0666
317 }).write(data.output+"\n");
318 }
319 });
320
321 logger.log('hello');
322 logger.trace('hello', 'world');
323 logger.debug('hello %s', 'world', 123);
324 logger.info('hello %s %d', 'world', 123, {foo:'bar'});
325 logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
326 logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
3feac15 @baryon fix format
authored Mar 2, 2012
327
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
328 ```
3feac15 @baryon fix format
authored Mar 2, 2012
329
2cbbaca @baryon npm public
authored Mar 2, 2012
330 ### MongoDB Transport
331 ```javascript
332 var mongo = require('mongoskin');
333 var db = mongo.db("127.0.0.1:27017/test?auto_reconnect");
334
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
335 var log_conf = {
8b3dfdb @baryon updated to 0.3.3
authored Mar 9, 2012
336 transport : function(data) {
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
337 console.log(data.output);
338 var loginfo = db.collection("loginfo");
339 loginfo.insert( data, function(err, log) {
340 if (err) {
341 console.error(err);
342 }
343 });
344 }
345 }
2cbbaca @baryon npm public
authored Mar 2, 2012
346
0cc9543 @baryon Add more examples: file, stream
authored Mar 5, 2012
347 var logger = require('tracer').console(log_conf);
348
349 logger.log('hello');
350 logger.trace('hello', 'world');
351 logger.debug('hello %s', 'world', 123);
352 logger.info('hello %s %d', 'world', 123, {foo:'bar'});
353 logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
354 logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
355
356 console.log('\n\n\npress ctrl-c to exit');
2cbbaca @baryon npm public
authored Mar 2, 2012
357
358 ```
359
360
00768a5 @baryon fixed some words
authored Mar 5, 2012
361 ### Define your logging helper
362
6a39a86 @baryon fixed some words
authored Mar 5, 2012
363 the work is like [color_console.js](https://github.com/baryon/tracer/blob/master/lib/color_console.js)
00768a5 @baryon fixed some words
authored Mar 5, 2012
364
365 ```javascript
366 var colors = require('colors');
367 module.exports = function(conf) {
368 return require('./console')({
369 filters : {
370 //log : colors.black,
371 trace : colors.magenta,
372 debug : colors.blue,
373 info : colors.green,
374 warn : colors.yellow,
375 error : [ colors.red, colors.bold ]
376 }
377 }, conf);
378 };
379 ```
2cbbaca @baryon npm public
authored Mar 2, 2012
380
381 ## History
382
6ed4997 @baryon updated to 0.5.1
authored Jun 27, 2012
383 ### 0.5.1
384
385 * feature: add args into data object, now we can check args in preprocess, see example format2.js, merge from yasuyk04/work
386
ce41ab1 @baryon updated to 0.5.0, added config.js stress.js examples
authored Jun 22, 2012
387 ### 0.5.0
388
389 * performance: speed up when the format don't include method,path,line,pos,file, thanks sharonjl's issue report
390
4896dc5 @baryon updated to 0.4.2
authored Apr 23, 2012
391 ### 0.4.2
392
393 * fixed: debug color from blue to cyan
394
385c782 @baryon updated to 0.4.1
authored Mar 21, 2012
395 ### 0.4.1
396
397 * fixed: default timestamp is ISO UTC format.
398
564cd2d @baryon updated to 0.4.0, add dailylog
authored Mar 21, 2012
399 ### 0.4.0
400
401 * feature: support dailyfile method, added some examples
402 * feature: add preprocess custom method for changing tags before format
403
6ce9897 @baryon updated to 0.3.5
authored Mar 14, 2012
404 ### 0.3.5
405
406 * fixed bug: can't get method/line number in express
407
f773aea @baryon updated to 0.3.4, use tinytim package
authored Mar 13, 2012
408 ### 0.3.4
409
410 * use [tinytim package](https://github.com/baryon/node-tinytim)
411
8b3dfdb @baryon updated to 0.3.3
authored Mar 9, 2012
412 ### 0.3.3
413
6158cf7 @baryon updated to 0.3.3
authored Mar 9, 2012
414 * spell missing (transpot->transport)
8b3dfdb @baryon updated to 0.3.3
authored Mar 9, 2012
415
ecb9d4b @baryon updated to 0.3.2
authored Mar 9, 2012
416 ### 0.3.2
417
418 * speed-up for _log function
419 * add some test codes
420
082dd42 @baryon minor-fix for call stack
authored Mar 8, 2012
421 ### 0.3.1
422
423 * minor-fix for call stack
424
b04fc40 @baryon updated to 0.3.0
authored Mar 7, 2012
425 ### 0.3.0
426
427 * support custom format and filter for special method
428
4e649e0 @baryon update to 0.2.1
authored Mar 5, 2012
429 ### 0.2.1
430
431 * fix spell missing
432
076d4da @baryon Add history to 0.2.0
authored Mar 5, 2012
433 ### 0.2.0
434
435 * Add more examples.
436 * Default methods is log, trace, debug, info, warn, error.
437 * Support 'string' level, {level:'warn'} equal {level:4}
2cbbaca @baryon npm public
authored Mar 2, 2012
438
439 ### 0.1.0
440
441 * Initial Tracer implementation.
442
443 ## License
444
445 (The MIT License)
446
f6b8f02 @baryon fixed format
authored Mar 2, 2012
447 Copyright (c) 2012 LI Long &lt;lilong@gmail.com&gt;
2cbbaca @baryon npm public
authored Mar 2, 2012
448
449 Permission is hereby granted, free of charge, to any person obtaining
450 a copy of this software and associated documentation files (the
451 'Software'), to deal in the Software without restriction, including
452 without limitation the rights to use, copy, modify, merge, publish,
453 distribute, sublicense, and/or sell copies of the Software, and to
454 permit persons to whom the Software is furnished to do so, subject to
455 the following conditions:
456
457 The above copyright notice and this permission notice shall be
458 included in all copies or substantial portions of the Software.
459
460 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
461 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
462 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
463 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
464 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
465 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
466 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Something went wrong with that request. Please try again.