Node.js trace_events.getEnabledCategories() 方法
trace_events.getEnabledCategories() (在 v10.0.0 中添加)方法是“trace_events”模块的内置应用程序编程接口,用于显示由所有当前启用的联合确定的当前启用的跟踪事件类别集跟踪对象。默认情况下,它的状态被 v8 运行时禁用,所有启用的类别都使用–trace-event-categories标志获取。
句法:
trace_events.getEnabledCategories()
为了使用此方法,我们需要使用 ( trace_events.createTracing() ) 方法创建跟踪事件,并且我们需要导入“trace_events”模块。
const trace_events = require('trace_events');
参数:此方法不接受任何参数。
返回值<字符串>:返回所有当前启用的跟踪事件类别的列表,以逗号分隔。
示例 1:文件名:index.js
// Node.js program to demonstrate the
// trace_events.getEnabledCategories() method
// Using require to access trace_events module
const trace_events = require('trace_events');
// Creating trace events
const trace_event1 = trace_events
.createTracing({ categories: ['node'] });
const trace_event2 = trace_events.createTracing({
categories: ['node.promises.rejections']
});
const { createTracing } = require('trace_events');
// Another way to create Tracing events
const trace_event3 = createTracing(
{ categories: ['node.vm.script'] });
// Enabling the trace_event1
trace_event1.enable();
// Enabling the trace_event2
trace_event2.enable();
// Printing the enabled categories
console.log("Enabled categories: ",
trace_events.getEnabledCategories());
// Disabling the trace_event1
trace_event1.disable();
// Disabling the trace_event2
trace_event2.disable();
输出:
Enabled categories node,node.promises.rejections
示例 2:文件名:index.js
// Node.js program to demonstrate the
// trace_events.getEnabledCategories() method
// Using require to access trace_events module
const trace_events = require('trace_events');
// Tracing categories
const categories = ['myapp.category', 'v8', 'node',
'node.async_hooks', 'node.promises.rejections',
'node.vm.script', 'node.perf.usertiming',
'node.perf.timerify'];
// Now create tracing for custom trace category.
const tracing = trace_events.createTracing({ categories });
// Enabling Tracing event
tracing.enable();
// Printing tracing event
console.log(">> ", tracing);
// Returns false if any tracing event is enabled
console.log(">> ", !trace_events.getEnabledCategories());
if (trace_events.getEnabledCategories()) {
console.log(">> Following tracing events are enabled",
trace_events.getEnabledCategories());
} else {
console.log(">> None of the Tracing events are enabled");
}
// Disabling tracing event
tracing.disable();
if (trace_events.getEnabledCategories()) {
console.log(">> Following tracing events are enabled",
trace_events.getEnabledCategories());
} else {
console.log(">> None of the Tracing events are enabled");
}
使用以下命令运行index.js文件:
node index.js
输出:
>> Tracing { enabled: true,
categories:’myapp.category,v8,node,node.async_hooks,node.promises.rejections,node.vm.script,node.perf.usertiming,node.perf.timerify’}
>> false
>> Following tracing events are enabled : myapp.category,node,node.async_hooks,node.perf.timerify,node.perf.usertiming,node.promises.rejections,node.vm.script,v8
>> None of the Tracing events are enabled
参考: https://nodejs.org/api/tracing.html#tracing_trace_events_getenabledcategories