24

Looking for a node.js package that handles stack tracing similar to how this is done in RoR:

Rails: Logging the entire stack trace of an exception

3

4 Answers 4

40

You can get this text off of the .stack property from any Error. For instance:

try {
    throw new Error();
} catch (e) {
    console.log(e.stack);
}

or just new up an error for the purposes of getting the stack trace

console.log(new Error().stack)
4
  • It would need to be (new Error()).stack. new Error().stack would get Error().stack and then try to construct something out of it
    – McKayla
    Commented Jul 10, 2012 at 4:20
  • 6
    no, that's not true in js the new operator always takes precedence.
    – Benja
    Commented Apr 11, 2013 at 3:07
  • 1
    You should actually use console.error instead of console.log. They may appear to do the same thing, but actually using the former outputs to stderr instead of stdout.
    – 0x8890
    Commented Oct 26, 2014 at 6:43
  • Especially useful in electron applications where console.trace() doesn't always work.
    – x-yuri
    Commented Jul 15, 2019 at 12:18
9

there's a function for that: console.trace()

In case you don't want to log to console you can get the stack trace string value using new Error().stack

4
  • I had no idea that method existed..I'll give this a try! Commented Mar 17, 2012 at 16:08
  • Ya, but what if you need to like.. iono.. pass it around, or do something with it? The console functions are called "helper" functions for a reason - they're just so you can be really lazy in specific cases ; )
    – B T
    Commented Apr 11, 2013 at 2:43
  • that sounds like a big "what if" based on original question, but let me update for that.
    – Benja
    Commented Apr 11, 2013 at 2:59
  • Not a big whatif at all. I report my error messages to a centralized logging store. Thank you much for the update! Commented Aug 30, 2013 at 22:10
9

If you use winston, you can add this:

winston = require('winston');

logger = expandErrors(new winston.Logger());

logger.info(new Error("my error"));

// Extend a winston by making it expand errors when passed in as the 
// second argument (the first argument is the log level).
function expandErrors(logger) {
  var oldLogFunc = logger.log;
  logger.log = function() {
    var args = Array.prototype.slice.call(arguments, 0);
    if (args.length >= 2 && args[1] instanceof Error) {
      args[1] = args[1].stack;
    }
    return oldLogFunc.apply(this, args);
  };
  return logger;
}

and then you get winston loggers with stack traces. Also is in a gist.

0

Check out callsite, this grabs the stack object so you can use it any way you like (and as an object, rather than a string). Its pretty awesome, and nice and simple. Check out this for more info on playing with the stack

There are a few excellent loggers out there for Node.js already, but I built a logger that outputs a colorized, simple output and brief stack trace. It doesn't override console.log, which I find to be handy. If you're interested, you can find it here, node-logger.

1
  • The implementation of callsite is consistent with the suggestion from @Benja. Commented Feb 1, 2017 at 18:53

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