question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Log caller function name and line number

See original GitHub issue

There does not seem to be a way to log the caller’s method name and line number. I know that this has already been requested with #200, but that issue was locked with the reason that such a function would be very slow. Given that a typical application would not excessively log thousands of log statements per second, it does not make a noticeably difference.

As you probably know, JavaScript provides the stack trace in the Error.stack property on construction of a new Error object. However, this is an unstructured string and needs to be parsed. V8 on the other hand has a feature allowing to retrieve the stack trace in its original structured form (before stringifying). This is described here: https://github.com/v8/v8/wiki/Stack-Trace-API

My current implementation on top of Winston looks like this. (It may not be a good implementation, I’m very new to JavaScript and Node/V8. 😃 )

// ES2015
const path = require('path');
const winston = require('winston');
function CustomError() {
  // Use V8's feature to get a structured stack trace
  const oldStackTrace = Error.prepareStackTrace;
  const oldLimit = Error.stackTraceLimit;
  try {
    Error.stackTraceLimit = 3; // <- we only need the top 3
    Error.prepareStackTrace = (err, structuredStackTrace) => structuredStackTrace;
    Error.captureStackTrace(this, CustomError);
    this.stack; // <- invoke the getter for 'stack'
  } finally {
    Error.stackTraceLimit = oldLimit;
    Error.prepareStackTrace = oldStackTrace;
  }
}
function getAndFormatStackTraceElement() {
  const stack = new CustomError().stack;
  const CALLER_INDEX = 2; // <- position in stacktrace to find deepest caller
  const element = stack[CALLER_INDEX];
  const fileName = path.basename(element.getFileName());
  return element.getFunctionName() + "(" + fileName + ":" + element.getLineNumber() + ")";
}
module.exports = {
  info(msg, ...vars) {
    winston.info(getAndFormatStackTraceElement() + ': ' + msg, ...vars);
  }
}

Credits go to @addaleax from: https://github.com/nodejs/node/issues/7749#issuecomment-232972234 The way the string is formatted and the module exports are defined, sure needs reworking, I guess.

This ticket could serve as a discussion on how a good PR would look like for Winston, adding this feature to the library.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
yeongjetcommented, Feb 8, 2020

please provide a option to enable this feature, like log4js

0reactions
indexzerocommented, Jan 29, 2019

Thanks for the suggestion @httpdigest! But as you said this is a duplicate of #200, so going to close it. Will leave the issue unlocked to keep the discussion going this time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

log C++ class method callers (function name, line number)
I want to log all the callers of myClass (function name, file name, line number). One possible solution is this: class myClass {...
Read more >
How to get the caller's function name, filename, and ... - SoByte
Learn how to get the caller's function name, filename, and line number in a Go function.
Read more >
Day of Swift: How to print class name, function name and line ...
In Swift, you can use #file #function line #column to get the debug info you want.
Read more >
Getting the line number and function name of a function caller
First to know the current line number, we have to throw a dummy exception, and catch it to decode its v:throwpoint . And...
Read more >
Function.prototype.caller - JavaScript - MDN Web Docs - Mozilla
prototype has an initial caller accessor that unconditionally throws a TypeError for any get or set request (known as a "poison pill accessor"), ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found