java debuglog_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

Java脚本

// 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);

使用以下命令运行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

Java脚本

// 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值