How to generate javascript stacktrace? [closed]
Asked Answered
F

3

8

Any suggestions on how to, in a cross-browser way, generate a stack trace in javascript?

Newer browsers, Chrome and Firefox, expose a console object that allows stack traces to be generated. This method does not provide a method for storing the stack trace to a variable.

https://github.com/eriwen/javascript-stacktrace Works quite nicely, but it makes separate ajax requests to load script files included as part of the trace. This seems to be a common method in trace libraries. I'm guessing that browsers do not expose enough information to generate a meaningful stack-trace(line numbers, function names, file names, arguments, etc).

Featherveined answered 20/11, 2012 at 19:23 Comment(2)
You are correct that IE9- (let's face it, that probably what we're having trouble with here) doesn't give much useful information. One could get the file and line number from window.onerror except that only fires some of the time and the line number is sometimes wrong.Tendance
Yes, as usual, IE is the problem child. I'm stuck supporting IE7 for which the onerror event's line number and function name information is terrible. It can still be useful; I use it to write logs for uncaught errors.Featherveined
R
2

Create an Error object and check it for a stack member. Adapted from Code Overtones:

var e = new Error('dummy');
var stack = e.stack.replace(/^[^\(]+?[\n$]/gm, '') // remove lines without '('
  .replace(/^\s+at\s+/gm, '') // remove prefix text ' at '
  .split('\n');
console.log(stack);

Error.stack is documented in Mozilla's reference documentation.

Rialto answered 2/5, 2013 at 22:15 Comment(0)
S
0

Airbrake provides a JavaScript library for logging stacktraces to your Airbrake account or Errbit server.

I don't get stack traces in IE, and others can be imperfect, but it definitely looks like it's along the lines of what you're looking for.

Seer answered 20/11, 2012 at 19:27 Comment(1)
Thanks. I already have a framework in place which can log to my server. It's basically log4j in javascript. The stack-trace generating code in the link you provided looks like it was borrowed from the project in my question. I'm looking for other libraries for stack-trace generation. The actual logging framework in use should be separate.Featherveined
V
-1

You can generate javascript stacktrace using stacktrace.js

http://stacktracejs.com/

Also, you can refer to : http://www.eriwen.com/javascript/js-stack-trace/

Vilberg answered 25/7, 2013 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.