8

Is there a way of specify stack trace depth when using console.trace? I would find this particularly useful for info and log level messages to identify line number and file

2 Answers 2

8

You can control how many stack frames are collected by setting the variable

Error.stackTraceLimit

Setting it to 0 will disable stack trace collection. Any finite integer value will be used as the maximum number of frames to collect. Setting it to Infinity means that all frames will be collected.

Another option is to use the command-line flag --stack-trace-limit:

node --stack-trace-limit=50 test.js

References:

http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi

1
  • The link is broken
    – ThaJay
    Dec 11, 2019 at 10:32
5

The method console.trace looks like:

exports.trace = function(label) {
  // TODO probably can to do this better with V8's debug object once that is
  // exposed.
  var err = new Error;
  err.name = 'Trace';
  err.message = label || '';
  Error.captureStackTrace(err, arguments.callee);
  console.error(err.stack);
};

https://github.com/joyent/node/blob/master/lib/console.js#L61

So it seems you can't set the trace depth.

1
  • What's arguments.callee?
    – Roy Toledo
    Jun 16, 2020 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.