1.0.6 • Published 11 months ago

@ahmadnassri/debug v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Node Debug

Debugging utility using environment regex, matches node core’s debugging technique

license release semantic

Install

npm install @ahmadnassri/debug

Usage

node-debug follows the exact same behaviour as util.debuglog with some minor differences:

  • checks for both DEBUG and NODE_DEBUG environment variables
  • checks environment variables everytime debug() is called (vs. on require)
  • regex matching in the environment variables
  • no global caching (you can change the behaviour after require everytime debug() is called)

debug(section)

  • section: <string> A string identifying the portion of the application for which the logging function is being created.
  • Returns: <Function> The logging function

debug() is used to create a function that conditionally writes debug messages to stderr based on the existence of the NODE_DEBUG or DEBUG environment variables. If the section name appears within the value of that environment variables, then the returned function operates similar to console.error(). If not, then the returned function is a no-op.

Example
const debuglog = require('@ahmadnassri/debug')
const debug = debuglog('foo')

debug('hello from foo [%d]', 123)

If this program is run with NODE_DEBUG=foo in the environment, then it will output something like:

FOO 3245: hello from foo [123]

where 3245 is the process id. If it is not run with that environment variable set, then it will not print anything.

Multiple Sections

Multiple section names may be specified in the NODE_DEBUG environment variable, separated by commas, or spaces.

Example
NODE_DEBUG=fs,net,tls

Regex Matching

section names in the NODE_DEBUG environment variable, can also be a regex string:

Example
const debuglog = require('@ahmadnassri/debug')
const primary = debuglog('foo:primary')
const secondary = debuglog('foo:secondary')

primary('primary logger')
secondary('secondary logger')
$ NODE_DEBUG=foo:.* node app.js

FOO:PRIMARY 28382: primary logger
FOO:SECONDARY 28382: secondary logger
Get Fancy
$ DEBUG="(F|O)+:.*" node app.js

FOO:PRIMARY 28274: primary logger
FOO:SECONDARY 28274: secondary logger

Author: Ahmad Nassri  Twitter: @AhmadNassri

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

12 months ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

7 years ago