Looking for a node.js package that handles stack tracing similar to how this is done in RoR:
-
What "stack trace package" in Rails are you talking about?– Andrew MarshallCommented Mar 4, 2012 at 1:57
-
I updated the question to add a link.– Chris AbramsCommented Mar 4, 2012 at 2:03
-
Possible duplicate of How to print a stack trace in Node.js?– Gustavo StraubeCommented Apr 29, 2016 at 13:15
4 Answers
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)
-
It would need to be
(new Error()).stack
.new Error().stack
would getError().stack
and then try to construct something out of it– McKaylaCommented Jul 10, 2012 at 4:20 -
6no, that's not true in js the
new
operator always takes precedence.– BenjaCommented Apr 11, 2013 at 3:07 -
1You should actually use
console.error
instead ofconsole.log
. They may appear to do the same thing, but actually using the former outputs tostderr
instead ofstdout
.– 0x8890Commented Oct 26, 2014 at 6:43 -
Especially useful in
electron
applications whereconsole.trace()
doesn't always work.– x-yuriCommented Jul 15, 2019 at 12:18
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
-
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 TCommented Apr 11, 2013 at 2:43
-
that sounds like a big "what if" based on original question, but let me update for that.– BenjaCommented 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
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.
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.
-
The implementation of callsite is consistent with the suggestion from @Benja. Commented Feb 1, 2017 at 18:53