debuglog().enabled


util.debuglog().enabled 获取器用于基于 NODE_DEBUG 环境变量的存在创建可用于条件的测试。 如果 section 名称出现在该环境变量的值中,则返回值将为 true。 如果不是,则返回值将是 false

const util = require('node:util');
const enabled = util.debuglog('foo').enabled;
if (enabled) {
  console.log('hello from foo [%d]', 123);
}

如果这个程序在环境中与 NODE_DEBUG=foo 一起运行,则它会输出如下内容:

hello from foo [123]

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.

const util = require('node: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:

hello from foo [123]