Node.js trace_events.getEnabledCategories() Method
Last Updated :
19 Aug, 2020
The trace_events.getEnabledCategories() (Added in v10.0.0) method is an inbuilt application programming interface of the ‘trace_events’ module which is used in showing the current set of enabled trace event categories that is determined by the union of all currently-enabled Tracing objects. By default, it’s status is disabled by v8 runtime and all enabled categories are fetch using the –trace-event-categories flag.
Syntax:
trace_events.getEnabledCategories()
In order to use this method, we need to create trace events using (trace_events.createTracing()) method, and we need to import the ‘trace_events’ module.
const trace_events = require('trace_events');
Parameters: This method does not accept any parameters.
Return Value <string>: It returns a list of all currently-enabled trace event categories which are separated by commas.
Example 1: Filename: index.js
const trace_events = require( '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' );
const trace_event3 = createTracing(
{ categories: [ 'node.vm.script' ] });
trace_event1.enable();
trace_event2.enable();
console.log( "Enabled categories: " ,
trace_events.getEnabledCategories());
trace_event1.disable();
trace_event2.disable();
|
Output:
Enabled categories node,node.promises.rejections
Example 2: Filename: index.js
const trace_events = require( 'trace_events' );
const categories = [ 'myapp.category' , 'v8' , 'node' ,
'node.async_hooks' , 'node.promises.rejections' ,
'node.vm.script' , 'node.perf.usertiming' ,
'node.perf.timerify' ];
const tracing = trace_events.createTracing({ categories });
tracing.enable();
console.log( ">> " , tracing);
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" );
}
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" );
}
|
Run index.js file using the following command:
node index.js
Output:
>> 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
Reference: https://nodejs.org/api/tracing.html#tracing_trace_events_getenabledcategories
Share your thoughts in the comments
Please Login to comment...