Permalink
Browse files

Add more examples: file, stream

  • Loading branch information...
1 parent fa2e09f commit 0cc9543c4bbef174dd6bbf14a9c0db8ffcf7d9ee @baryon committed Mar 5, 2012
Showing with 209 additions and 78 deletions.
  1. +111 −21 README.md
  2. +2 −2 example/colorConsole.js
  3. +2 −2 example/console.js
  4. +20 −0 example/file.js
  5. +5 −11 example/format.js
  6. +2 −4 example/level.js
  7. +8 −2 example/methods.js
  8. +18 −18 example/mongodb.js
  9. +19 −0 example/stream.js
  10. +2 −1 lib/color_console.js
  11. +15 −12 lib/console.js
  12. +2 −2 package.json
  13. +3 −3 test/test.js
View
132 README.md
@@ -40,19 +40,25 @@ var logger = require('tracer').colorConsole({level:2});
Simple Example
--------------
+some helper package is need, so install -dev for running examples
+
+```javascript
+npm install -dev tracer
+```
+
### Simple Console
```javascript
var logger = require('tracer').console();
-logger.test('hello');
+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);
$ node example/console.js
-2012-03-02T13:35:22.83Z <test> console.js:3 (Object.<anonymous>) hello
+2012-03-02T13:35:22.83Z <log> console.js:3 (Object.<anonymous>) hello
2012-03-02T13:35:22.85Z <trace> console.js:4 (Object.<anonymous>) hello world
2012-03-02T13:35:22.85Z <debug> console.js:5 (Object.<anonymous>) hello world 123
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
### Color Console
```javascript
var logger = require('tracer').colorConsole();
-logger.test('hello');
+logger.log('hello');
logger.trace('hello', 'world');
logger.debug('hello %s', 'world', 123);
logger.info('hello %s %d', 'world', 123, {foo:'bar'});
@@ -77,18 +83,30 @@ Advanced Example
Take a look at the examples directory for different examples.
### Set logging level
+
+the level option support index (number) or method name.
+
+
+```javascript
+var logger = require('tracer').console({level:'warn'});
+```
+equal
+
+```javascript
+var logger = require('tracer').console({level:4});
+```
+
+
```javascript
-var logger = require('tracer').console({level:2});
-logger.test('hello');
+var logger = require('tracer').console({level:'warn'});
+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);
//$ node example/level.js
-//2012-03-02T13:41:33.17Z <debug> level.js:4 (Object.<anonymous>) hello world 123
-//2012-03-02T13:41:33.28Z <info> level.js:5 (Object.<anonymous>) hello world 123 { foo: 'bar' }
//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] }
@@ -102,7 +120,7 @@ logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
format tag:
* timestamp: current time
-* title: method name, default is 'test', 'trace', 'debug', 'info', 'warn', 'error'
+* title: method name, default is 'log', 'trace', 'debug', 'info', 'warn', 'error'
* message: printf message, support %s string, %d number, %j JSON and auto inspect
* file: file name
* line: line number
@@ -127,14 +145,30 @@ var logger = require('tracer').console(
### Customize methods and filters
-each filtes function was called. the function must be like
+each filtes function was called. the function is synchronous and must be like
```javascript
function f1(str) {
return str.toUpperCase();
}
```
+or you can use the second parameter
+
+```javascript
+function f1(str, data) {
+ if( data.title === 'log5' ){
+ // data.title === 'error' or data.title === 'warn'
+ //do some thing, example write to database, you can use async write to do this
+
+ //if you don't want contine other filter, then
+ //return false;
+ }
+ return str.toUpperCase();
+}
+```
+
+
About [Colors.js](https://github.com/Marak/colors.js)
```javascript
@@ -159,26 +193,82 @@ logger.log5('hello %s %d', 'world', 123);
```
+### Log File Transport
+```javascript
+var fs = require('fs');
+
+var logger = require('tracer').console({
+ transpot : function(data) {
+ console.log(data.output);
+ fs.open('./file.log', 'a', 0666, function(e, id) {
+ fs.write(id, data.output+"\n", null, 'utf8', function() {
+ fs.close(id, function() {
+ });
+ });
+ });
+ }
+});
+
+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);
+
+```
+
+### Stream Transport
+```javascript
+var fs = require('fs');
+
+var logger = require('tracer').console({
+ transpot : function(data) {
+ console.log(data.output);
+ var stream = fs.createWriteStream("./stream.log", {
+ flags: "a",
+ encoding: "utf8",
+ mode: 0666
+ }).write(data.output+"\n");
+ }
+});
+
+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);
+```
### MongoDB Transport
```javascript
var mongo = require('mongoskin');
var db = mongo.db("127.0.0.1:27017/test?auto_reconnect");
-//var logger = require('tracer').console();
+var log_conf = {
+ transpot : function(data) {
+ console.log(data.output);
+ var loginfo = db.collection("loginfo");
+ loginfo.insert( data, function(err, log) {
+ if (err) {
+ console.error(err);
+ }
+ });
+ }
+}
-var dblogger = require('tracer').console({
- transpot : function(data) {
- //logger.info(data.output);
- var loginfo = db.collection("loginfo");
- loginfo.insert( data, function(err, log) {
- if (err) {
- //logger.error(err);
- }
- });
- },
-});
+var logger = require('tracer').console(log_conf);
+
+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);
+
+console.log('\n\n\npress ctrl-c to exit');
```
View
4 example/colorConsole.js
@@ -1,14 +1,14 @@
var logger = require('tracer').colorConsole();
-logger.test('hello');
+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);
//$ node example/colorConsole.js
-//2012-03-02T13:43:09.27Z <test> colorConsole.js:3 (Object.<anonymous>) hello
+//2012-03-02T13:43:09.27Z <log> colorConsole.js:3 (Object.<anonymous>) hello
//2012-03-02T13:43:09.29Z <trace> colorConsole.js:4 (Object.<anonymous>) hello world
//2012-03-02T13:43:09.29Z <debug> colorConsole.js:5 (Object.<anonymous>) hello world 123
//2012-03-02T13:43:09.29Z <info> colorConsole.js:6 (Object.<anonymous>) hello world 123 { foo: 'bar' }
View
4 example/console.js
@@ -1,6 +1,6 @@
var logger = require('tracer').console();
-logger.test('hello');
+logger.log('hello');
logger.trace('hello', 'world');
logger.debug('hello %s', 'world', 123);
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);
//$ node example/console.js
-//2012-03-02T13:42:00.99Z <test> console.js:3 (Object.<anonymous>) hello
+//2012-03-02T13:42:00.99Z <log> console.js:3 (Object.<anonymous>) hello
//2012-03-02T13:42:01.02Z <trace> console.js:4 (Object.<anonymous>) hello world
//2012-03-02T13:42:01.03Z <debug> console.js:5 (Object.<anonymous>) hello world 123
//2012-03-02T13:42:01.05Z <info> console.js:6 (Object.<anonymous>) hello world 123 { foo: 'bar' }
View
20 example/file.js
@@ -0,0 +1,20 @@
+var fs = require('fs');
+
+var logger = require('tracer').console({
+ transpot : function(data) {
+ console.log(data.output);
+ fs.open('./file.log', 'a', 0666, function(e, id) {
+ fs.write(id, data.output+"\n", null, 'utf8', function() {
+ fs.close(id, function() {
+ });
+ });
+ });
+ }
+});
+
+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
16 example/format.js
@@ -5,22 +5,16 @@ var logger = require('tracer')
dateformat : "HH:MM:ss.L"
});
-logger.test('hello');
+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);
+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);
//$ node example/format.js
-//13:52:30.61 <test> hello (in format.js:7)
+//13:52:30.61 <log> hello (in format.js:7)
//13:52:30.63 <trace> hello world (in format.js:8)
//13:52:30.63 <debug> hello world 123 (in format.js:9)
//13:52:30.63 <info> hello world 123 { foo: 'bar' } (in format.js:10)
View
6 example/level.js
@@ -1,13 +1,11 @@
-var logger = require('tracer').console({level:2});
-logger.test('hello');
+var logger = require('tracer').console({level:'warn'});
+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);
//$ node example/level.js
-//2012-03-02T13:41:33.17Z <debug> level.js:4 (Object.<anonymous>) hello world 123
-//2012-03-02T13:41:33.28Z <info> level.js:5 (Object.<anonymous>) hello world 123 { foo: 'bar' }
//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] }
View
10 example/methods.js
@@ -1,13 +1,19 @@
var colors = require('colors');
-function f1(str) {
+function f1(str, data) {
+ if( data.title === 'log5' ){
+ //do some thing, example write to database
+
+ //if you don't want contine other filter, then
+ return false;
+ }
return str.toUpperCase();
}
var logger = require('tracer').colorConsole({
level : 1,
methods : [ 'log0', 'log1', 'log2', 'log3', 'log4', 'log5' ],
- filters : [f1, colors.bold, colors.italic, colors.underline, colors.inverse, colors.yellow],
+ filters : [f1, colors.underline, colors.yellow],
});
logger.log0('hello');
View
36 example/mongodb.js
@@ -1,25 +1,25 @@
var mongo = require('mongoskin');
var db = mongo.db("127.0.0.1:27017/test?auto_reconnect");
-//var logger = require('tracer').console();
+var log_conf = {
+ transpot : function(data) {
+ console.log(data.output);
+ var loginfo = db.collection("loginfo");
+ loginfo.insert( data, function(err, log) {
+ if (err) {
+ console.error(err);
+ }
+ });
+ }
+}
-var dblogger = require('tracer').console({
- transpot : function(data) {
- //logger.info(data.output);
- var loginfo = db.collection("loginfo");
- loginfo.insert( data, function(err, log) {
- if (err) {
- //logger.error(err);
- }
- });
- },
-});
+var logger = require('tracer').console(log_conf);
-dblogger.test('hello');
-dblogger.trace('hello', 'world');
-dblogger.debug('hello %s', 'world', 123);
-dblogger.info('hello %s %d', 'world', 123, {foo:'bar'});
-dblogger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
-dblogger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
+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);
console.log('\n\n\npress ctrl-c to exit');
View
19 example/stream.js
@@ -0,0 +1,19 @@
+var fs = require('fs');
+
+var logger = require('tracer').console({
+ transpot : function(data) {
+ console.log(data.output);
+ var stream = fs.createWriteStream("./stream.log", {
+ flags: "a",
+ encoding: "utf8",
+ mode: 0666
+ }).write(data.output+"\n");
+ }
+});
+
+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
3 lib/color_console.js
@@ -2,7 +2,8 @@ var colors = require('colors');
module.exports = function(conf) {
return require('./console')({
filters : {
- test : colors.magenta,
+ //log : colors.black,
+ trace : colors.magenta,
debug : colors.blue,
info : colors.green,
warn : colors.yellow,
View
27 lib/console.js
@@ -8,8 +8,8 @@ module.exports = (function() {
console.log(data.output);
},
filters : [],
- level : 0,
- methods : [ 'test', 'trace', 'debug', 'info', 'warn', 'error' ]
+ level : 'log',
+ methods : [ 'log', 'trace', 'debug', 'info', 'warn', 'error' ]
};
var _union = function(obj, args) {
@@ -25,9 +25,7 @@ module.exports = (function() {
_config = _union(_config, arguments);
- var _log = function(level, title, args) {
- if (level < _config.level)
- return;
+ var _log = function(title, args) {
var msg = msgformat.apply(this, args);
var data = {
timestamp : dateFormat(new Date(), _config.dateformat),
@@ -60,7 +58,7 @@ module.exports = (function() {
filters = Array.isArray(filters) ? filters : [ filters ];
var i, len = filters.length;
for (i = 0; i < len; i += 1) {
- data.output = filters[i](data.output);
+ data.output = filters[i](data.output, data);
if (!data.output)
return data;
}
@@ -72,14 +70,19 @@ module.exports = (function() {
var _self = {};
- var len = _config.methods.length
+ var len = _config.methods.length;
+ if(typeof(_config.level)=='string')
+ _config.level=_config.methods.indexOf(_config.level);
for ( var i = 0; i < len; ++i) {
var method = _config.methods[i];
- _self[method] = (function(level, title) {
- return (function() {
- return _log(level, title, arguments);
- });
- })(i, method);
+ if (i < _config.level)
+ _self[method] = (function(){});
+ else
+ _self[method] = (function(title) {
+ return (function() {
+ return _log(title, arguments);
+ });
+ })(method);
}
return _self;
View
4 package.json
@@ -1,8 +1,8 @@
{
"name": "tracer",
- "description": "A powerful and customizable logging library for node.js. keywords: log, logger, trace ",
+ "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.1.0",
+ "version": "0.2.0",
"author": "LI Long <lilong@gmail.com>",
"dependencies": {
"dateformat": "1.0.2-1.2.3",
View
6 test/test.js
@@ -8,7 +8,7 @@ exports["simple message"] = function() {
return data;
}
});
- var o = logger.test('hello');
+ var o = logger.log('hello');
assert.equal(o['output'], 'hello');
}
@@ -20,6 +20,6 @@ exports["simple console message"] = function() {
return data;
}
});
- var o = logger.test('hello');
- assert.equal(o.output, '\u001b[35mhello\u001b[39m');
+ var o = logger.debug('hello');
+ assert.equal(o.output, '\u001b[34mhello\u001b[39m');
}

0 comments on commit 0cc9543

Please sign in to comment.