Node.js util.debuglog() 方法
“ util ”模块提供了用于调试目的的“ utility ”函数。为了访问这些函数,我们需要调用它们(通过' require('util') ')。
util.debuglog() (在 v0.11.3 中添加)方法是 util 模块的内置应用程序编程接口,用于创建基于NODE_DEBUG环境变量的函数,该函数有条件地将调试消息写入stderr 。如果节名称出现在该环境变量的值中,则返回的函数的操作方式与console.error()相同。如果不是,则返回的函数是no-op 。
句法:
util.debuglog(section[, callback])
参数:该函数接受上面提到的两个参数,如下所述:
- section < 字符串 >:它接受一个 < 字符串 > 标识正在为其创建调试日志函数的应用程序部分。
- callback < 函数 >:它接受第一次调用日志函数时调用的回调,函数参数是更优化的日志函数..
返回值< 函数 >:返回记录函数。
示例 1:文件名:index.js
Javascript
// Node.js program to demonstrate the
// util.debuglog() method
// Using require to access util module
const util = require('util');
const debugLog = util.debuglog('run-app');
// Use debuglog() method
debugLog('hello from my debugger [%d]', 123);
// SET NODE_DEBUG=run-app&&node util.js
// Another way to import debuglog
const { debuglog } = require('util');
const debuglogue = debuglog('run-app1');
// Use debuglog() method
debuglogue('hello from run-app [%d]', 123);
const a = "old Value";
let deebuglog = util.debuglog('run-app2',
(debuging) => {
// Replace with a logging function
// that optimizes out
a = "new Value";
// Testing if the section is enabled
deebuglog = debuging;
});
// prints the debuglog function
console.log(util.inspect(deebuglog,
showHidden = true, compact = true));
// Prints nothing
console.log(a);
// logs app *
deebuglog();
deebuglog('hi there, it\'s run-app [%d]', 2333);
Javascript
// Node.js program to demonstrate the
// util.debuglog() method
// Using require to access util module
const util = require('util');
// const {debuglog} = require('util');
const debuglog = util.debuglog('alfa-beta');
debuglog('Hii there, debuglog from alfa-beta [%d]', 2333);
const generalLog = util.debuglog('alfa-');
const timerLog = util.debuglog('alfa-romeo');
const delay = 800;
generalLog('Leaving alfa-...');
console.log("Wait for timerLog...")
setTimeout(() => {
timerLog('timer fired after %d ', delay);
}, delay);
使用以下命令运行index.js文件:
SET NODE_DEBUG=run-app*&&node index.js
OR
NODE_DEBUG=run-app* node index.js
输出:
>> RUN-APP 8112: hello from my debugger [123]
>> RUN-APP1 8112: hello from run-app [123]
>> [Function (anonymous)] {…… [prototype]: { [constructor]: [Circular *1] }
}
>> old Value
>> RUN-APP2 8112:
>> RUN-APP2 8112: hi there, it’s run-app [2333]
示例 2:文件名:index.js
Javascript
// Node.js program to demonstrate the
// util.debuglog() method
// Using require to access util module
const util = require('util');
// const {debuglog} = require('util');
const debuglog = util.debuglog('alfa-beta');
debuglog('Hii there, debuglog from alfa-beta [%d]', 2333);
const generalLog = util.debuglog('alfa-');
const timerLog = util.debuglog('alfa-romeo');
const delay = 800;
generalLog('Leaving alfa-...');
console.log("Wait for timerLog...")
setTimeout(() => {
timerLog('timer fired after %d ', delay);
}, delay);
使用以下命令运行index.js文件:
SET NODE_DEBUG=alfa-*&&node index.js
OR
NODE_DEBUG=alfa-* node index.js
输出:
ALFA-BETA 7008: Hii there, debuglog from alfa-beta [2333]
ALFA- 7008: Leaving alfa-…
Wait for timerLog…
ALFA-ROMEO 7008: timer fired after 800
参考: https://nodejs.org/api/util.html#util_util_debuglog_section_callback