當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js trace_events.getEnabledCategories()用法及代碼示例


trace_events.getEnabledCategories()(在 v10.0.0 中添加)方法是 ‘trace_events’ 模塊的內置應用程序編程接口,用於顯示由所有 currently-enabled 跟蹤對象的並集確定的當前啟用的跟蹤事件類別集。默認情況下,它的狀態被 v8 運行時禁用,所有啟用的類別都使用 -trace-event-categories 標誌獲取。

用法:

trace_events.getEnabledCategories()

為了使用這個方法,我們需要使用 (trace_events.createTracing()) 方法創建跟蹤事件,我們需要導入 ‘trace_events’ 模塊。

const trace_events = require('trace_events');  

參數:此方法不接受任何參數。

返回值<string>:返回以逗號分隔的所有 currently-enabled 跟蹤事件類別的列表。



範例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




相關用法


注:本文由純淨天空篩選整理自vikas_g大神的英文原創作品 Node.js trace_events.getEnabledCategories() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。