Skip to content
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

Closed
httpdigest opened this issue Jul 15, 2016 · 6 comments
Closed

Getting the stack trace in a structured form #7749

httpdigest opened this issue Jul 15, 2016 · 6 comments
Labels
question Issues that look for answers. v8 engine Issues and PRs related to the V8 dependency.

Comments

@httpdigest
Copy link

httpdigest commented Jul 15, 2016

  • Version:7.0.0-pre (commit 45367a2)
  • Platform:Windows 7 x64
  • Subsystem:?

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?

@addaleax
Copy link
Member

Is this currently possible in Node?

Yes! :) It sounds like https://github.com/v8/v8/wiki/Stack-Trace-API#customizing-stack-traces is exactly what you are looking for.

@addaleax addaleax added question Issues that look for answers. v8 engine Issues and PRs related to the V8 dependency. labels Jul 15, 2016
@httpdigest
Copy link
Author

Thanks for this awesome link! :)
However, I tried it and it does not seem to work. Can you please tell me what I am doing wrong here?

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:

E
undefined

So error.stack always seems to be in string format.

@addaleax
Copy link
Member

You need to override Error.prepareStackTrace to actually get the stack in a different format:

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();

@httpdigest
Copy link
Author

httpdigest commented Jul 15, 2016

Wow. Very nice! Thank you! :)

@addaleax
Copy link
Member

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

@httpdigest
Copy link
Author

Yes, it is resolved. I did not know about nodejs/help, though. Will ask any further questions there, then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers. v8 engine Issues and PRs related to the V8 dependency.
Projects
None yet
Development

No branches or pull requests

2 participants