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

Community
  • 1
  • 1
Chris Abrams
  • 39,732
  • 19
  • 51
  • 57

4 Answers4

39

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)
kelloti
  • 8,705
  • 5
  • 46
  • 82
  • 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 Jul 10 '12 at 04:20
  • 6
    no, that's not true in js the ``new`` operator always takes precedence. – Benja Apr 11 '13 at 03: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 Oct 26 '14 at 06:43
  • Especially useful in `electron` applications where `console.trace()` doesn't always work. – x-yuri Jul 15 '19 at 12:18
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.

jdg
  • 2,230
  • 2
  • 20
  • 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

Benja
  • 4,099
  • 1
  • 30
  • 31
  • I had no idea that method existed..I'll give this a try! – Chris Abrams Mar 17 '12 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 Apr 11 '13 at 02:43
  • that sounds like a big "what if" based on original question, but let me update for that. – Benja Apr 11 '13 at 02:59
  • Not a big whatif at all. I report my error messages to a centralized logging store. Thank you much for the update! – Michael Lang Aug 30 '13 at 22:10
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.

Mike P
  • 2,797
  • 2
  • 21
  • 25