Skip to content

Prepend function name and line number to debug message #343

Closed

Description

@hutch120

Hi All,

Love this project! Sorry if this has already been covered... I did take a look at the issue list and found mostly people saying not implemented, or suggesting browser related solutions. I'm working in NodeJS, and having a function name and line number on each debug message really helps development efforts.

I'm wondering what the implications of adding the following which I've just started using in a recent NodeJS project? Especially the use of arguments.callee which I've read is depreciated. Is there an alternative?

I would love to be able to turn this on as an option, rather than have to append it to every debug message as shown below.

const debug = require('debug')('MyApp:MyModule')
debug(__dstring + "My debug message")

Outputs: MyApp:MyModule myFunction 44 My debug message

__dstring is defined like this:

Object.defineProperty(global, '__dstring', {
  get: function () {
    try {
      var orig = Error.prepareStackTrace;
      Error.prepareStackTrace = function (_, stack) {
        return stack;
      };
      var err = new Error;
      Error.captureStackTrace(err, arguments.callee);
      var stack = err.stack;
      Error.prepareStackTrace = orig;
      return stack[1].getMethodName() + " " + stack[1].getLineNumber() + " ";
    } catch (err) {
      return ""
    }
  }
});

References:
http://stackoverflow.com/questions/11386492/accessing-line-number-in-v8-javascript-chrome-node-js

Activity

  1. likun7981 commented on Dec 13, 2016

    @likun7981

    how about #346

  2. thebigredgeek commented on Dec 14, 2016

    @thebigredgeek
    Contributor

    #370 < seems like a solid case for V3 middleware API :). Feel free to jump into the conversation

  3. added
    wont-fixThis is not a feature or proposal that will be incorporated, or a bug that won't be addressed
    on Dec 14, 2016
  4. Leftium commented on Mar 5, 2026

    @Leftium
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    wont-fixThis is not a feature or proposal that will be incorporated, or a bug that won't be addressed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions