Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updated to 0.3.0

  • Loading branch information...
commit b04fc40437737e77b571f35e096ec9772de2b0a5 1 parent 6a39a86
@baryon authored
View
73 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 <warn> level.js:6 (Object.<anonymous>) hello world 123 {"foo":"bar"}
//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] }
-// 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 <log1> methods.js:10 (Object.<anonymous>) hello world
-//2012-03-02T13:49:36.90Z <log2> methods.js:11 (Object.<anonymous>) hello world 123
-//2012-03-02T13:49:36.91Z <log4> methods.js:12 (Object.<anonymous>) hello world 123
-//2012-03-02T13:49:36.91Z <log5> methods.js:13 (Object.<anonymous>) 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
View
29 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);
View
18 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);
View
22 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 <LOG1> METHODS.JS:14 (OBJECT.<ANONYMOUS>) HELLO WORLD
-//2012-03-02T14:10:31.46Z <LOG2> METHODS.JS:15 (OBJECT.<ANONYMOUS>) HELLO WORLD 123
-//2012-03-02T14:10:31.46Z <LOG4> METHODS.JS:16 (OBJECT.<ANONYMOUS>) HELLO WORLD 123
-//2012-03-02T14:10:31.46Z <LOG5> METHODS.JS:17 (OBJECT.<ANONYMOUS>) HELLO WORLD 123
+//2012-03-07T10:09:16.23Z <log1> methods.js:10 (Object.<anonymous>) hello world
+//2012-03-07T10:09:16.24Z <log2> methods.js:11 (Object.<anonymous>) hello world 123
+//2012-03-07T10:09:16.24Z <log4> methods.js:12 (Object.<anonymous>) hello world 123
+//2012-03-07T10:09:16.24Z <log5> methods.js:13 (Object.<anonymous>) hello world 123
View
6 file.log
@@ -0,0 +1,6 @@
+2012-03-07T11:38:33.54Z <log> file.js:15 (Object.<anonymous>) hello
+2012-03-07T11:38:33.55Z <trace> file.js:16 (Object.<anonymous>) hello world
+2012-03-07T11:38:33.55Z <debug> file.js:17 (Object.<anonymous>) hello world 123
+2012-03-07T11:38:33.55Z <info> file.js:18 (Object.<anonymous>) hello world 123 { foo: 'bar' }
+2012-03-07T11:38:33.55Z <warn> file.js:19 (Object.<anonymous>) hello world 123 {"foo":"bar"}
+2012-03-07T11:38:33.55Z <error> file.js:20 (Object.<anonymous>) hello world 123 {"foo":"bar"} [ 1, 2, 3, 4 ] function Object() { [native code] }
View
27 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 = '<unkown>';
+ data.method = data.path = data.line = data.pos = data.file = '<unknown>';
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)
View
2  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 <lilong@gmail.com>",
"dependencies": {
"dateformat": "1.0.2-1.2.3",
View
51 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');
}
Please sign in to comment.
Something went wrong with that request. Please try again.