megasniff
v1.0.0
Published
Better debugging for promise chains and pipelines
Downloads
5
Maintainers
Readme
Megasniff
Better debugging for chained functions (promises, streams, etc.). Inspired by supersniff, but with these improvements in mind:
- [x] Support for custom loggers (like bunyan, winston, or other services)
- [x] Pre- and suffix support
- [x] No forced
tag
- [x] Using error levels for errors
Things to do:
- [ ] Custom configureable log levels
- [ ] Decorator functions (e.g.
error => error.message
)
Motivation
Can be found here. It shall be a readable replacement for:
getData()
.then(data => {
console.info(data)
return data
})
.then(transformData)
Installation
npm install megasniff
Examples
const megasniff = require('megasniff')
const reveersedList = new Promise(resolve => resolve([3, 1, 2]))
.then(megasniff)
.then(list => list.sort())
.then(megasniff)
.then(list => list.reverse())
.then(megasniff)
This yields in the console:
[ 3, 1, 2 ]
[ 1, 2, 3 ]
[ 3, 2, 1 ]
const megasniff = require('megasniff')
new Promise(resolve => resolve('Good'))
.then(megasniff.config({ prefix: `Value at ${new Date()}:` }))
This yields on stdout:
Good Value at Wed Oct 17 2018 15:47:26 GMT+0200 (Central European Summer Time)
const megasniff = require('megasniff')
const logger = console
new Promise((resolve, reject) => reject(new Error('Bad')))
.catch(megasniff.config({ suffix: '.. We are condemned' }))
.catch(error => error)
This yields on stderr:
Error: Bad ... We are doomed
API
Default Usage:
const megasniff = require('megasniff')
const sortedList = new Promise(resolve => resolve([3, 1, 2]))
.then(megasniff)
.then(list => list.sort())
.then(megasniff)
megasniff
is a function that logs and returns the passed value.
The value is logged either to stdout or sterr depending on the passed value.
Configuration:
const megasniff = require('megasniff')
const logger = console
new Promise(resolve => resolve('Good Software'))
.then(megasniff.config({ prefix: `[megasniff]`, logger }))
megasniff.config
takes an object as argument.
Megasniff can be configured with these optional parameters:
| key | value | |--------|-----------------------------------| | log | External logger | | logger | Other key for external logger | | prefix | Will be prefixed the logged value | | suffix | Will be suffixed the logged value |