debug-fn
Wraps visionmedia/debug to execute a function on demand based on the DEBUG env variable
Install
npm install --save-dev debug-fn
Usage
A logger is created in the same way as debug, the difference is that the logger receives a function instead of multiple arguments, the function will be called based on debug rules (which checks the value of the DEBUG env variable)
// file example.js const logger =
Running example.js
with the DEBUG env set to app
$ DEBUG=app node example.js app hello! +0ms
Running example.js
without the DEBUG env doesn't log anything
$ node example.js
NOTE: you could also use this.log
inside logger's function argument (which is
the same as the log
argument)
// file example.js const logger =
If you want to overwrite debug's custom logger, the logger instance is stored in
logger.logger
e.g.
// logger.logger is the result of calling `debug('app')`loggerlogger { // custom logic to handle the output}
Why?
Imagine you have a cpu expensive operation that you only want to be executed when DEBUG is set, debug can't do that out of the box, this wrapper allows you to do that by wrapping your cpu expensive operation in a function
License
MIT © Mauricio Poppe