Node.js console.trace() Method

The console.trace() method is an inbuilt application programming interface of the console module which is used to print stack trace messages to stderr in a newline. Similar to console.error() method.

Syntax:

console.trace(message, args);

Parameters: This method has two parameters as mentioned above and described below:

  1. message: This parameter specifies the message to be printed.
  2. args: It is optional parameter that specifies the parameters to be passed as substitution values in the message. All passed args are sent to util.format().

Return Value: This method doesn’t return anything but print ‘Trace:’ string followed by the formatted message to stderr in a newline and stack trace to the current position in the code.

Below examples illustrate the use of console.trace() method in Node.js:



Example 1:
Filename: app.js

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the 
// console.trace() method
  
// Accessing console module
const console = require('console');
   
// Calling console.trace() method
console.trace("stack teace sample");
  
console.trace(
    "stack trace sample with args: %d", 39);

chevron_right


Run the app.js file using the following command:

node app.js

Output:

Trace: stack teace sample
at Object. (C:\nodejs\g\console\console_trace.js:4:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Trace: stack trace sample with args: 39
at Object. (C:\nodejs\g\console\console_trace.js:5:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Example 2:
Filename: app.js

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the 
// console.trace() method
  
// Accessing console module
const console = require('console');
   
// Calling console.trace() method
console.trace("stack trace message: "
    + "at %s: line no: %d ", "ff()", 96);
   
var isTrace = true;
  
console.custom_trace = function(message) {   
  if (isTrace) {
    console.trace(message);
  }
}
  
console.custom_trace("custom trace message");

chevron_right


Run the app.js file using the following command:

node app.js

Output:

Trace: stack trace message: at ff(): line no: 96
at Object. (C:\nodejs\g\console\console_trace_1.js:4:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Trace: custom trace message
at Console.console.custom_trace (C:\nodejs\g\console\console_trace_1.js:11:13)
at Object. (C:\nodejs\g\console\console_trace_1.js:14:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Note: The above program will compile and run by using the node filename.js command.

Reference: https://nodejs.org/api/console.html#console_console_trace_data_args




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.