How to generate javascript stacktrace? [closed]

Deadly 提交于 2019-12-21 10:00:23

问题


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).


回答1:


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.




回答2:


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.




回答3:


You can generate javascript stacktrace using stacktrace.js

http://stacktracejs.com/

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



来源:https://stackoverflow.com/questions/13480558/how-to-generate-javascript-stacktrace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!