-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting the stack trace in a structured form #7749
Comments
Yes! :) It sounds like https://github.com/v8/v8/wiki/Stack-Trace-API#customizing-stack-traces is exactly what you are looking for. |
Thanks for this awesome link! :) function CustomError() {
Error.captureStackTrace(this);
}
function myFunction() {
const err = new CustomError();
console.log(err.stack[0]);
console.log(err.stack[0].getFunctionName);
}
myFunction(); The output is:
So error.stack always seems to be in string format. |
You need to override function CustomError() {
const oldStackTrace = Error.prepareStackTrace;
try {
Error.prepareStackTrace = (err, structuredStackTrace) => structuredStackTrace;
Error.captureStackTrace(this);
this.stack; // Invoke the getter for `stack`.
} finally {
Error.prepareStackTrace = oldStackTrace;
}
}
function myFunction() {
const err = new CustomError();
console.log(err.stack[0]);
console.log(err.stack[0].getFunctionName);
}
myFunction(); |
Wow. Very nice! Thank you! :) |
I’m taking that to mean that your question has been answered, so I’m closing this, but feel free to ask follow-up questions (although this might have been better placed at https://github.com/nodejs/help). |
Yes, it is resolved. I did not know about nodejs/help, though. Will ask any further questions there, then. |
This is more like a question. But, after delving ever deeper into Node.js development and looking into logging and logging frameworks like Winston, I was kind of shocked that neither provide ways to log the function name and line number of a log callsite.
There is an issue for Winston winstonjs/winston#200 which, after having received alot of +1's, was apparently locked and their contributors are somewhat reluctant to add such essential logging capabilities into it.
Coming from Java and Java EE, I perceived logging the function name and line number as the most important thing in big-scale applications.
After reading some replies as to how people currently do it, there seems to be the way of getting the current stack trace with
new Error().stack
. But that gives a string representation of the stack trace elements. So, we have to decode that string again. I reckon that there existed a more structured representation of the stack before it was "stringified". So it would make sense to get hold of this structured information somehow.Is this currently possible in Node?
The text was updated successfully, but these errors were encountered: