debug-instanced
v1.0.0
Published
Like the 'debug' module, but automatically labels output with an instance ID. Useful when dealing with many different objects.
Readme
debug-instanced
Like the debug module, but automatically labels output with an instance ID. Useful when dealing with many different objects.
Usage
See also example.js in the package.
"use strict";
const debugInstanced = require("debug-instanced")("example");
function doThing() {
let debug = debugInstanced("foo");
debug("line one");
2 + 2;
debug("line two");
}
doThing();
doThing();This would output the following:
~/projects/debug-instanced> DEBUG=* node example.js
example [e04ab8] created instance (type: foo) +0ms
example [e04ab8] line one +1ms
example [e04ab8] line two +0ms
example [931af6] created instance (type: foo) +0ms
example [931af6] line one +1ms
example [931af6] line two +1msAs you can see, the output from each 'instance' of the call to doThing is labelled with its own unique ID, that is generated when debugInstanced is called.
The API is basically the same as that of debug, except there's an extra level of function invocation. Normally when you call the function exported by debug you immediately get a function that lets you log things; but in debug-instanced, that gets you an instance creation function instead. That instance creation function, when called, returns the logging function you need - and it's called the same way as the logging function in debug.
The enabled property is available on the instance creation function, instead of on the logging function where it would be in debug.
