log API for nodejs for new house projects based on nodejs is used for generate logs which more friendly to Centrailzed Logging System1
log API for nodejs is based on tracer
https://xmsvn.ehealthinsurance.com/svn/centralized-logging/branches/dev/log-client-node
####pattern
level:{{title}}|$|timestamp:{{timestamp}}|$|requestId:{{requestId}}|$|class:{{file}}|$|line:{{line}}|$|message:{{message}}
####example
level:info|$|timestamp:2014091617010569|$|requestId:|$|class:index.js|$|line:12|$|message:hello world!
add dependency to package.json
"dependencies": {
"log-client-node": "0.0.2",
}
install log-client-node
npm install
require log-client-node in your code
app.log = require('log-client-node')(setting);
Here is the setting example
setting = {};
setting.APP_NAME = 'xis';
setting.logDirectory = 'logs';
setting.strategy = 'console'; // [console, colorConsole, dailyfile]
setting.level = 'info';
log API for nodejs will create a temp log file beside the original log file. For example: If the original log file named "2014-09-10-xis-node.log" then there will be a log file named "2014-09-10-xis.log.temp" be created.
log-client-node will create log file which name is in this format logDate + "-" + appName + "-node.log"
.
for example
2014-06-08-xis-node.log
log-client-node support 4 levels logs like log4j: debug
, info
, warn
, error
####example
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);
log-client-node supports log transaction. It can helps you to print the duration time of one operation, such as database IO, read file, send request, etc.
####Usage
- Use
log.newTransaction(transactionContext)
to create a log transaction. transactionContext can be aString
or aJSON
Object. - Use
log.completeTransaction()
at the end of this operation. Then it will print a log with your transactionContext and the duration time.
NOTE:
####example
app.log.info('Begin to query code table, sql: select * from code_product_type ');
app.log.newTransaction('sql: select * from code_product_type');
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.1.33',
port : 3306,
user : 'admin',
password : 'lkjhgf',
database : 'test'
});
connection.connect();
connection.query('select * from code_product_type', function(err, rows, fields) {
if (err) throw err;
app.log.completeTransaction();
res.send({ 'hello': 'world1' });
});
connection.end();
####Result
level:info|$|timestamp:2014091716555489|$|requestId:|$|class:D:\WebstormProjects\log-client-node-demo\index.js|$|line:15|$|message:hello log-api
level:info|$|timestamp:2014091716555490|$|requestId:|$|class:D:\WebstormProjects\log-client-node-demo\index.js|$|line:17|$|message:Begin to query code table, sql: select * from code_prod
uct_type
level:info|$|timestamp:2014091716555575|$|requestId:|$|class:D:\WebstormProjects\log-client-node\index.js|$|line:122|$|message:{transContext:"sql: select * from code_product_type",durati
on:847}
-
Centralized Logging System is a log collect system. You can query all logs generated by ehi project, whatever new house or old house. Engineers can use this system to do troubleshooting. ↩