From bcfb1762a3e613e71ac68ab8a6420e2f33c0f603 Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Fri, 12 Jun 2020 10:25:38 -0500 Subject: [PATCH] util: add debug and debuglog.enabled PR-URL: https://github.com/nodejs/node/pull/33424 Reviewed-By: James M Snell --- doc/api/util.md | 36 +++++++++++++++++++++++ lib/internal/util/debuglog.js | 47 ++++++++++++++++++++++-------- lib/util.js | 1 + test/sequential/test-util-debug.js | 4 +-- 4 files changed, 74 insertions(+), 14 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index 4d0b4a863e29dd..325cf9cde2e6e7 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -136,6 +136,42 @@ let debuglog = util.debuglog('internals', (debug) => { }); ``` +### `debuglog().enabled` + + +* {boolean} + +The `util.debuglog().enabled` getter is used to create a test that can be used +in conditionals based on the existence of the `NODE_DEBUG` environment variable. +If the `section` name appears within the value of that environment variable, +then the returned value will be `true`. If not, then the returned value will be +`false`. + +```js +const util = require('util'); +const enabled = util.debuglog('foo').enabled; +if (enabled) { + console.log('hello from foo [%d]', 123); +} +``` + +If this program is run with `NODE_DEBUG=foo` in the environment, then it will +output something like: + +```console +hello from foo [123] +``` + +## `util.debug(section)` + + +Alias for `util.debuglog`. Usage allows for readability of that doesn't imply +logging when only using `util.debuglog().enabled`. + ## `util.deprecate(fn, msg[, code])`