In the documentation for visionmedia's debug
module for Node.js, I see that it is possible to enable and disable different debug targets programmatically.
My attempts to do this have not been very successful.
I created a barebones script: test.js
const debug = require("debug");
let test = debug("test"); // created before being enabled
console.log(1, debug.enabled("test"));
let namespaces = debug.enable("test");
// test = debug("test");
console.log(2, debug.enabled("test"));
console.log("enabled namespaces:", namespaces);
test("this is test");
namespaces = debug.disable();
// test = debug("test");
console.log(3, debug.enabled("test"));
console.log("disabled namespaces:", namespaces);
test("you shouldn't see this");
When I run the script using node test.js
, I get the following output, which shows that debug.enable("test")
has neither enabled the test
debug target, nor has it returned any namespaces:
1 false
2 true
enabled namespaces: undefined
3 false
disabled namespaces: undefined
When I run the script using DEBUG=test node test.js
, I get the following output, which shows that debug.disable("test")
has neither disabled the test
debug target, nor has it returned the names of any disabled namespaces:
1 true
2 true
enabled namespaces: undefined
test this is test +0ms
3 false
disabled namespaces: undefined
test you shouldn't see this +0ms
However, if I uncomment the lines that recreate the test
debug target after using .enable()
or .disable()
, then everything works.
It seems that, in order to be able to enable and disable debug targets programatically, I need to use a different syntax:
debug("test")("message which can be enabled or disabled");
But it seems that this creates a new debug instance each time it is used, which would be wasteful. And it is not the standard syntax described in the documentation. And, even so, the .enabled()
and .disabled()
methods do not return a list of namespaces.
Is there something that I have badly misunderstood?