项目作者: ioncache

项目描述 :
Generate log object compatible with log-defer-viz (https://github.com/hoytech/Log-Defer-Viz)
高级语言: JavaScript
项目地址: git://github.com/ioncache/log-defer.git
创建时间: 2016-02-02T20:24:13Z
项目社区:https://github.com/ioncache/log-defer

开源协议:MIT License

下载


log-defer

Unit Tests codecov

Description

Generate log object compatible with log-defer-viz

log-defer is a module that creates structured logs. The log-defer documentation explains structured logging and its benefits over ad-hoc logging.

This is the javascript implementation, full documentation for log-defer can be found at https://metacpan.org/pod/Log::Defer

Installation

  1. npm install log-defer

or

  1. bower install log-defer

Synopsis

  1. 'use strict';
  2. var log = require('log-defer');
  3. var process = require('process');
  4. log.timer('Log Messages'); // begin a timer called 'Log Messages'
  5. log.info('Info level logging');
  6. log.warn('Warn level logging');
  7. log.error('Error level logging');
  8. log.debug('Debug level logging');
  9. log.timer('Log Messages'); // end the 'Log Messages' timer
  10. log.data({ bar: 'baz', foo: 'bar' }); // add to the data object in the log-defer
  11. log.warn('Warning with data!', { bar: 'baz', foo: 'bar' }, { barbaz: 'bazfoo', foobar: 'barbaz' });
  12. var output = log.finalizeLog(); // finalize the log, and return a json string of the log-defer
  13. process.stdout.write(output);

The above code should produce the following output:

  1. {"data":{"bar":"baz","foo":"bar"},"logs":[[0.0009999275207519531,30,"Info level logging"],[0.0009999275207519531,20,"Warn level logging"],[0.0009999275207519531,10,"Error level logging"],[0.0009999275207519531,40,"Debug level logging"],[0.0009999275207519531,20,"Warning with data!",{"bar":"baz","foo":"bar","barbaz":"bazfoo","foobar":"barbaz"}]],"start":1457018806.914,"timers":[["Log Messages",0.0009999275207519531,0.0009999275207519531]],"end":0.0019998550415039062}

If viewed using the log-defer-viz cli then it would format as follows:

  1. ------ 2016-03-03 Thu 10:26:46.914 EST (1457018806.914) ------
  2. | 0.001000 [ INFO] Info level logging
  3. | 0.001000 [ WARN] Warn level logging
  4. | 0.001000 [ERROR] Error level logging
  5. | 0.001000 [DEBUG] Debug level logging
  6. | 0.001000 [ WARN] Warning with data! [{"foo":"bar","bar":"baz","foobar":"barbaz","barbaz":"bazfoo"}]
  7. |_0.002000 [END]
  8. Log Messages X
  9. _______________________________________________________________________________________________________
  10. times in ms 1.0
  11. Data:
  12. {
  13. "bar" : "baz",
  14. "foo" : "bar"
  15. }