Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Enable/disable on runtime for Node.js #419

Closed
moehlone opened this issue Feb 3, 2017 · 8 comments
Closed

[Question] Enable/disable on runtime for Node.js #419

moehlone opened this issue Feb 3, 2017 · 8 comments

Comments

@moehlone
Copy link

moehlone commented Feb 3, 2017

Hi,

is there a way to enable or disable debugging for modules on runtime (Node.js)?

Best regards
Philipp

@360disrupt
Copy link

I'm not sure if I understood your question. But you could modify the env. variable in the code e.g.
process.env.DEBUG="error*,-fatal"

If you listen for console inputs than you could modify it from your console:

 process.stdin.on "data", (command) ->
        command = command.toString().trim()
        console.log "now displaying:  ",command
        process.env.DEBUG = command

@moehlone
Copy link
Author

moehlone commented Feb 7, 2017

I think that this wont be enough, because the logging mechanism is initialized on first require with environment variable DEBUG. Lets say I want to modify the namespace on different HTTP-Requests within my routes, when a special header value was set:

Request-Header:
x-some-debug: "express*, error*"

function getSomething(req, res) {
    process.env.DEBUG = req.headers["x-some-debug"];
    // log everything with express* and error*
    // ...
    process.env.DEBUG = "";
    // restore anything to default after request finished..
    res.send("Hello!");
}

Assignment to process.env.DEBUG was only for example.. Is there any way to change or reinitialize these values during runtime?

@DoumanAsh
Copy link

DoumanAsh commented Feb 20, 2017

The work around you can try is to clean require cache?
Normally it is available through require.cache[moduleName]

P.s. if you take a look at source code https://github.com/visionmedia/debug/blob/master/src/node.js#L241

@moehlone
Copy link
Author

moehlone commented Mar 7, 2017

@DoumanAsh: might work, but no solution for me.. thank you anyway.

@DoumanAsh
Copy link

@moehlone I linked in my PS how debug inits logging on first import.
I think method enable should be exported and you can do it by yourself anytime you want like debug.enable(process.env.DEBUG)

@geonanorch
Copy link

@moehlone it sounds like we have similar needs: I want to be able to dynamically (through some kind of listener, e.g. file watching or socket) change the debug settings.
Until now the debug module does not keep track of its offspring, but a small change in src/debug.js fixes that, here a diff -n:

a21 1
exports._debuggers = Object.create(null);
a126 1
  exports._debuggers[namespace] = debug;
a155 10
  for (const namespace in exports._debuggers) {
      exports._debuggers[namespace].enabled = false;
      exports.names.some( (pattern) => {
          const match = pattern.test(namespace);
          if (match) {
              exports._debuggers[namespace].enabled = true;
          }
          return match;
      });
  }

you can then alter dynamically debug settings by passing the same value as in the DEBUG environment variable:

const debugCore = require('debug');
debugCore.enable('mymod*');

For listeners I am thinking to use file watching, found an example with pinojs/pino-arborsculpture

And by the way I recommend pinojs/pino-debug, it is compatible wiith debug (including the change I described above) and reportedly much faster.

I have raised issue #433 in the hope to get the above code change (or similar) integrated.

@moehlone
Copy link
Author

@geonanorch thank you - this is what we thought about. I think #433 is enough and we can close this one.

@manjeshpv
Copy link

Enable/Disable Dynamically runtime easily using https://gist.github.com/manjeshpv/106dc2e05d4b9f252dc9f3f41afccb59

@Qix- Qix- pinned this issue Feb 24, 2021
@Qix- Qix- unpinned this issue Feb 24, 2021
@debug-js debug-js locked as off-topic and limited conversation to collaborators Feb 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

5 participants