2

I'd like to use debug to be able to print method / function names and arguments on every call.

What's the best way to achieve this instead of placing customized debug statements in every function like this:

async function getFilePaths(path, id) {
  debug(`async function getFilePaths(path = ${path}, id = ${id})`);
  // ...
}
5
  • When writing an object to debug in Chrome it allows to view all it's properties Commented Jul 16, 2015 at 5:21
  • 2
    @AmirGeri this is server side JS - node Commented Jul 16, 2015 at 5:22
  • If you want to know where you are at a certain step, you can do console.trace(), though I only recommend using it in the console window. I haven't tested with IE10+, but console.trace() and console.debug() broke IE 9 unless the developer window was open. Commented Jul 16, 2015 at 5:23
  • now that I see it's node.js, my comment is probably not related. Commented Jul 16, 2015 at 5:23
  • wrap your functions with a logging pass-through function Commented Jul 16, 2015 at 5:26

1 Answer 1

1

You could do something like this:

function logDebug(fn, args) {
   debug(fn.name + args.toString());
}

And call it in your function

function getFilePaths(path, id) {
  logDebug(getFilePaths, arguments);
}

Note: Function.name is a new technology supported in ES6.

Sign up to request clarification or add additional context in comments.

3 Comments

This is closer to what I want. arguments.toString() doesn't play well with debug, so it should be Array.slice(arguments). However I still need to specify function name explicitly. Something I'd like to avoid.
You should not slice on arguments because it prevents optimizations in JavaScript engines (V8 for example). Instead, try constructing a new array by iterating through the arguments object. And you could try function.caller but it's not supported widely. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
This is for debugging purposes, so I'm not concerned about performance. Using debug alone slows the whole thing down quite a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.