debuglog().enabled


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

【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.】

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

如果在环境中使用 NODE_DEBUG=foo 运行此程序,它将输出类似如下内容:

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

hello from foo [123]