1.0.3 • Published 8 years ago

debug-fn v1.0.3

Weekly downloads
10
License
MIT
Repository
github
Last release
8 years ago

debug-fn

NPM version Build Status

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
import debugFn from 'debug-fn'

const logger = debugFn('app')

logger(function (log) {
  log('hello')
})

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
import debugFn from 'debug-fn'

const logger = debugFn('app')

logger(function () {
  this.log('hello')
})

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')`
logger.logger.log = function () {
  // 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