visionmedia/debug — pull request #73 by lzrski (2013-11-21 19:40:47+00:00)
Title: fn.narrow() method to create new debug function based on existing one.

Hi,

would you please take a look at this enhancement to Debug?

In node I use namespaces a lot with debug, often like that:

``` javascript
var $ = debug ("myProject");
$ ("Woohoo! Starting app!");

var doStuff = function (...) {
var $ = debug("myProject:doStuff");
$ ("Doing some stuff");
}
doSomethingAsync (function myCallback (...) {
$ = debug("myProject:somethingAsync");
$ ("Doing something async");
doStuff();
});
```

As you can guess it quickly gets very repetitive (`myProject:doSomething:doSomethingNested:...` - several levels deep) and error prone.

So I came up with idea to have something like:

``` javascript
var $ = debug ("myProject");
$ ("Starting app");
var doStuff = function(...) {
var $ = $.narrow("doStuff");
$ ("Doing it!"); // myProject:doStuff: Doing it!
}
$ ("Something else"); // myProject: Something else
```

I called it `narrow` because it narrows the scope of debug messages.

As a side product I had to add `.scope` property which indicates current scope (eg. `"myProject:doStuff"`), and `.root` property which points to a first function created with call to `debug()`. Every function created with `.narrow()` has `.root` pointing to the *mother function*. So inside `doStuff` you can do for example `$.root.scope` to get `"myProject"`, or $.root("msg") to call root debug function directly.

Please let me know what do you think.