https://github.com/ioncache/log-defer
Generate log object compatible with log-defer-viz (https://github.com/hoytech/Log-Defer-Viz)
https://github.com/ioncache/log-defer
javascript javascript-library logger logger-interface logging npm
Last synced: 18 days ago
JSON representation
Generate log object compatible with log-defer-viz (https://github.com/hoytech/Log-Defer-Viz)
- Host: GitHub
- URL: https://github.com/ioncache/log-defer
- Owner: ioncache
- License: mit
- Created: 2016-02-02T20:24:13.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2023-04-24T10:58:00.000Z (about 2 years ago)
- Last Synced: 2025-04-10T00:43:35.582Z (18 days ago)
- Topics: javascript, javascript-library, logger, logger-interface, logging, npm
- Language: JavaScript
- Homepage:
- Size: 201 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE
Awesome Lists containing this project
README
# log-defer
 [](https://codecov.io/gh/ioncache/log-defer)
## Description
Generate log object compatible with [log-defer-viz](https://github.com/hoytech/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
## Installation
``` bash
npm install log-defer
```or
```bash
bower install log-defer
```## Synopsis
```javascript
'use strict';var log = require('log-defer');
var process = require('process');log.timer('Log Messages'); // begin a timer called 'Log Messages'
log.info('Info level logging');
log.warn('Warn level logging');
log.error('Error level logging');
log.debug('Debug level logging');
log.timer('Log Messages'); // end the 'Log Messages' timer
log.data({ bar: 'baz', foo: 'bar' }); // add to the data object in the log-deferlog.warn('Warning with data!', { bar: 'baz', foo: 'bar' }, { barbaz: 'bazfoo', foobar: 'barbaz' });
var output = log.finalizeLog(); // finalize the log, and return a json string of the log-defer
process.stdout.write(output);
```The above code should produce the following output:
```bash
{"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:
```bash
------ 2016-03-03 Thu 10:26:46.914 EST (1457018806.914) ------
| 0.001000 [ INFO] Info level logging
| 0.001000 [ WARN] Warn level logging
| 0.001000 [ERROR] Error level logging
| 0.001000 [DEBUG] Debug level logging
| 0.001000 [ WARN] Warning with data! [{"foo":"bar","bar":"baz","foobar":"barbaz","barbaz":"bazfoo"}]
|_0.002000 [END]Log Messages X
_______________________________________________________________________________________________________
times in ms 1.0Data:
{
"bar" : "baz",
"foo" : "bar"
}
```