diff --git a/README.md b/README.md index dbf8f22..f609193 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object); //2012-03-02T13:41:33.29Z level.js:6 (Object.) hello world 123 {"foo":"bar"} //2012-03-02T13:41:33.30Z level.js:7 (Object.) hello world 123 {"foo":"bar"} [ 1, 2, 3, 4 ] function Object() { [native code] } -// log,trace, debug and info level was not ouputed +//log,trace, debug and info level was not ouputed ``` @@ -149,8 +149,44 @@ var logger = require('tracer').console( ``` +Or, you can set special format for output method -### Customize methods and filters +```javascript +var logger = require('tracer') + .colorConsole( + { + format : [ + "{{timestamp}} <{{title}}> {{message}} (in {{file}}:{{line}})", //default format + { + // error format + error : "{{timestamp}} <{{title}}> {{message}} (in {{file}}:{{line}})\nCall Stack: {{stack}}" + } + ], + dateformat : "HH:MM:ss.L" + }); +``` + +### Customize output methods + +```javascript +var colors = require('colors'); + +var logger = require('tracer').colorConsole({ + level : 'log1', + methods : [ 'log0', 'log1', 'log2', 'log3', 'log4', 'log5' ], + filters : [colors.underline, colors.yellow], + +}); +logger.log0('hello'); +logger.log1('hello', 'world'); +logger.log2('hello %s', 'world', 123); +logger.log4('hello %s %d', 'world', 123); +logger.log5('hello %s %d', 'world', 123); +``` + + + +### Customize filters each filtes function was called. the function is synchronous and must be like @@ -164,8 +200,7 @@ or you can use the second parameter ```javascript function f1(str, data) { - if( data.title === 'log5' ){ - // data.title === 'error' or data.title === 'warn' + if( data.title === 'error' ){ //do some thing, example write to database, you can use async write to do this //if you don't want continue other filter, then @@ -181,26 +216,19 @@ About [Colors.js](https://github.com/Marak/colors.js) ```javascript var colors = require('colors'); var logger = require('tracer').colorConsole({ - level : 1, - methods : [ 'log0', 'log1', 'log2', 'log3', 'log4', 'log5' ], - filters : [colors.bold, colors.italic, colors.underline, colors.inverse, colors.yellow], - + filters : [ + f1, colors.underline, colors.blue, //default filter + //the last item can be custom filter. here is "warn" and "error" filter + { + warn : colors.yellow, + error : [f1, colors.red, colors.bold ] + } + ] }); -logger.log0('hello'); -logger.log1('hello', 'world'); -logger.log2('hello %s', 'world', 123); -logger.log4('hello %s %d', 'world', 123); -logger.log5('hello %s %d', 'world', 123); - -//$ node example/methods.js -//2012-03-02T13:49:36.89Z methods.js:10 (Object.) hello world -//2012-03-02T13:49:36.90Z methods.js:11 (Object.) hello world 123 -//2012-03-02T13:49:36.91Z methods.js:12 (Object.) hello world 123 -//2012-03-02T13:49:36.91Z methods.js:13 (Object.) hello world 123 ``` -the filter support key-value pair, example: [color_console.js](https://github.com/baryon/tracer/blob/master/lib/color_console.js) +the filter support key-function pair, example: [color_console.js](https://github.com/baryon/tracer/blob/master/lib/color_console.js) ```javascript { @@ -215,6 +243,7 @@ the filter support key-value pair, example: [color_console.js](https://github.co } ``` +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) ### Log File Transport ```javascript @@ -318,6 +347,10 @@ module.exports = function(conf) { ## History +### 0.3.0 + +* support custom format and filter for special method + ### 0.2.1 * fix spell missing diff --git a/example/filter.js b/example/filter.js new file mode 100644 index 0000000..6d838c2 --- /dev/null +++ b/example/filter.js @@ -0,0 +1,29 @@ +var colors = require('colors'); + +function f1(str, data) { + if( data.title === 'error' ){ + // do some thing, ex: write to database + + // if you don't want continue other filter, then + //return false; + } + return str.toUpperCase(); +} + +var logger = require('tracer').colorConsole({ + filters : [ + f1, colors.underline, colors.blue, //default filter + //the last item can be custom filter. here is "warn" and "error" filter + { + warn : colors.yellow, + error : [f1, colors.red, colors.bold ] + } + ] +}); + +logger.log('hello'); +logger.trace('hello', 'world'); +logger.debug('hello %s', 'world', 123); +logger.info('hello %s %d', 'world', 123, {foo:'bar'}); +logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'}); +logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object); diff --git a/example/format2.js b/example/format2.js new file mode 100644 index 0000000..0aa924d --- /dev/null +++ b/example/format2.js @@ -0,0 +1,18 @@ +var logger = require('tracer') + .colorConsole( + { + format : [ + "{{timestamp}} <{{title}}> {{message}} (in {{file}}:{{line}})", //default format + { + error : "{{timestamp}} <{{title}}> {{message}} (in {{file}}:{{line}})\nCall Stack: {{stack}}" // error format + } + ], + dateformat : "HH:MM:ss.L" + }); + +logger.log('hello'); +logger.trace('hello', 'world'); +logger.debug('hello %s', 'world', 123); +logger.info('hello %s %d', 'world', 123, {foo : 'bar'}); +logger.warn('hello %s %d %j', 'world', 123, {foo : 'bar'}); +logger.error('hello %s %d %j', 'world', 123, {foo : 'bar'}, [ 1, 2, 3, 4 ], Object); diff --git a/example/methods.js b/example/methods.js index 5986805..ecc101b 100644 --- a/example/methods.js +++ b/example/methods.js @@ -1,19 +1,9 @@ var colors = require('colors'); -function f1(str, data) { - if( data.title === 'log5' ){ - //do some thing, example write to database - - //if you don't want continue other filter, then - return false; - } - return str.toUpperCase(); -} - var logger = require('tracer').colorConsole({ - level : 1, + level : 'log1', methods : [ 'log0', 'log1', 'log2', 'log3', 'log4', 'log5' ], - filters : [f1, colors.underline, colors.yellow], + filters : [colors.underline, colors.yellow], }); logger.log0('hello'); @@ -23,7 +13,7 @@ logger.log4('hello %s %d', 'world', 123); logger.log5('hello %s %d', 'world', 123); //$ node example/methods.js -//2012-03-02T14:10:31.45Z METHODS.JS:14 (OBJECT.) HELLO WORLD -//2012-03-02T14:10:31.46Z METHODS.JS:15 (OBJECT.) HELLO WORLD 123 -//2012-03-02T14:10:31.46Z METHODS.JS:16 (OBJECT.) HELLO WORLD 123 -//2012-03-02T14:10:31.46Z METHODS.JS:17 (OBJECT.) HELLO WORLD 123 \ No newline at end of file +//2012-03-07T10:09:16.23Z methods.js:10 (Object.) hello world +//2012-03-07T10:09:16.24Z methods.js:11 (Object.) hello world 123 +//2012-03-07T10:09:16.24Z methods.js:12 (Object.) hello world 123 +//2012-03-07T10:09:16.24Z methods.js:13 (Object.) hello world 123 diff --git a/file.log b/file.log new file mode 100644 index 0000000..d1ae759 --- /dev/null +++ b/file.log @@ -0,0 +1,6 @@ +2012-03-07T11:38:33.54Z file.js:15 (Object.) hello +2012-03-07T11:38:33.55Z file.js:16 (Object.) hello world +2012-03-07T11:38:33.55Z file.js:17 (Object.) hello world 123 +2012-03-07T11:38:33.55Z file.js:18 (Object.) hello world 123 { foo: 'bar' } +2012-03-07T11:38:33.55Z file.js:19 (Object.) hello world 123 {"foo":"bar"} +2012-03-07T11:38:33.55Z file.js:20 (Object.) hello world 123 {"foo":"bar"} [ 1, 2, 3, 4 ] function Object() { [native code] } diff --git a/lib/console.js b/lib/console.js index b66cc16..d4ab7c5 100644 --- a/lib/console.js +++ b/lib/console.js @@ -25,6 +25,12 @@ module.exports = (function() { _config = _union(_config, arguments); + _config.format = Array.isArray(_config.format) ? _config.format : [_config.format]; + + var _isFunction = function (x) { + return Object.prototype.toString.call(x) == '[object Function]'; + }; + var _log = function(title, args) { var msg = msgformat.apply(this, args); var data = { @@ -32,7 +38,7 @@ module.exports = (function() { message : msg, title : title }; - data.method = data.path = data.line = data.pos = data.file = ''; + data.method = data.path = data.line = data.pos = data.file = ''; try { throw new Error(); } catch (e) { @@ -49,14 +55,25 @@ module.exports = (function() { data.file = paths[paths.length - 1]; } } + var format = _config.format[0]; + if(_config.format.length===2 && _config.format[1][title]) + format = _config.format[1][title]; - data.output = tim(_config.format, data); + data.output = tim(format, data); if (_config.filters) { var filters = Array.isArray(_config.filters) ? _config.filters - : _config.filters[title]; - if (filters) { - filters = Array.isArray(filters) ? filters : [ filters ]; + : [_config.filters]; + if (filters.length>0) { var i, len = filters.length; + if(!_isFunction(filters[len-1])){ + len -= 1; + if(filters[len][title]){ + filters = filters[len][title] + filters = Array.isArray(filters) ? filters : [ filters ]; + len = filters.length; + } + } + for (i = 0; i < len; i += 1) { data.output = filters[i](data.output, data); if (!data.output) diff --git a/package.json b/package.json index 47031df..97d3897 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "tracer", "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 ", "homepage": "http://github.com/baryon/tracer", - "version": "0.2.1", + "version": "0.3.0", "author": "LI Long ", "dependencies": { "dateformat": "1.0.2-1.2.3", diff --git a/test/test.js b/test/test.js index b16f747..7724e37 100644 --- a/test/test.js +++ b/test/test.js @@ -36,4 +36,55 @@ exports["console log method"] = function() { assert.equal(o['title'], 'log'); assert.equal(o['file'], 'test.js'); assert.equal(o['output'], 'hello world 123'); +} + +exports["custom format"] = function() { + var logger = require('../').console({ + format : [ + "{{message}}", //default format + { + warn : "warn:{{message}}", + error : "error:{{message}}", + } + ], + transpot : function(data) { + console.log(data.output); + return data; + } + }); + var o = logger.log('hello %s %d', 'world', 123); + assert.equal(o['output'], 'hello world 123'); + o = logger.warn('hello %s %d', 'world', 123); + assert.equal(o['output'], 'warn:hello world 123'); + o = logger.error('hello %s %d', 'world', 123); + assert.equal(o['output'], 'error:hello world 123'); +} + +exports["custom filter"] = function() { + var colors = require('colors'); + var logger = require('../').console({ + format : [ + "{{message}}", //default format + { + warn : "warn:{{message}}", + error : "error:{{message}}", + } + ], + filters:[ + colors.underline, + { + warn : colors.yellow, + error : [colors.red, colors.bold ] + }], + transpot : function(data) { + console.log(data.output); + return data; + } + }); + var o = logger.log('hello %s %d', 'world', 123); + assert.equal(o['output'], '\u001b[4mhello world 123\u001b[24m'); + o = logger.warn('hello %s %d', 'world', 123); + assert.equal(o['output'], '\u001b[33mwarn:hello world 123\u001b[39m'); + o = logger.error('hello %s %d', 'world', 123); + assert.equal(o['output'], '\u001b[1m\u001b[31merror:hello world 123\u001b[39m\u001b[22m'); } \ No newline at end of file