Node.js – util.debuglog() 方法
util.debuglog() 方法创建一个函数,该函数可用于将所需的错误/调试消息写入 stderr。只有在存在 NODE_DEBUG 环境变量时,才写入这些错误消息。
语法
util.debuglog(section, [callback])
参数
参数如下文所述 −
section − 此参数采用创建调试日志的应用程序部分。
callback − 这是将接收执行方法期间发生任何错误的指针的回调函数。
示例 1
创建一个名为 "debuglog.js" 的文件并复制以下代码片段 -
// util.debuglog() demo example
// Importing the util module
const util = require('util');
const debugLog = util.debuglog('application#1');
// Use debuglog() method
debugLog('Welcome to Tutorials Point');
let debug = util.debuglog('application#2',
(debuging) => {
// Replace with a logging function
// that optimizes out
a = "new Value";
// Testing if the section is enabled
debug = debuging;
});
// prints the debuglog function
console.log(util.inspect(debug,
showHidden = true, compact = true));
// logs app *
debug();
debug('Hello User #[%d]', 2);使用以下命令在调试模式下运行上述应用程序 -
NODE_DEBUG=application* node debuglog.js
输出
C:\home
ode>> NODE_DEBUG=application* node debuglog.js APPLICATION#1 337356: Welcome to Tutorials Point { [Function: debug] [length]: 0, [name]: 'debug', [prototype]: debug { [constructor]: [Circular] } } APPLICATION#2 337356: APPLICATION#2 337356: Hello User #[2]
示例 2
让我们再看一个示例 -
// util.debuglog() demo example
// Importing the util module
const util = require('util');
const debuglog = util.debuglog('application');
debuglog('Hello [%s], - From Tutorials Point', 'user');
const generalLog = util.debuglog('app-');
const timerLog = util.debuglog('appl');
const delay = 200;
// Printing the log for 'app-'
generalLog('Exiting app-');
console.log("Waiting for Timer Log to get executed")
// Timer will run after 200 ms
setTimeout(() => {
timerLog('Timer is fired after %d ', delay);
}, delay);输出
C:\home
ode>> NODE_DEBUG=application* node debuglog.js APPLICATION 338111: Hello [user], - From Tutorials Point APP- 338111: Exiting appWaiting for Timer Log to get executed APPL 338111: Timer is fired after 200
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP