question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redirect debug output from required modules?

See original GitHub issue

Hey all, I have a NodeJS-application in which I’m trying to redirect the debug output from modules I require. Not sure if I’m doing something wrong or what I want is not possible, so I thought I’d ask.

My situation is as follows:

App => has a custom debug log I want to redirect the logs to
|-- Module A => logs debug messages with debug("namespace-a")(message...)
    |-- Module B => logs debug messages with debug("namespace-b")(message...)

In the app, I am using the following code to replace the log function of the sub modules with my custom one:

process.env.DEBUG = "*";
const debugPackage = require("debug");
const debuggers = [debugPackage("namespace-a"), debugPackage("namespace-b")];
for (const d of debuggers) {
    d.log = (str) => customLogFunction(str);
}

However I’m not getting any redirected logs. Am I missing something?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
TooTallNatecommented, Sep 29, 2017

The problem is that when you do debugPackage("namespace-a") it creates a new debug instance every time, so it’s not going to affect another “namespace-a” instance that is in a different file.

Instead, you can overwrite the debugPackage.log function in the same way, which will affect all created debug instances.

debugPackage.log = customLogFunction;
0reactions
AlCalzonecommented, Oct 1, 2017

npm ls debug gives:

├─┬ iobroker.tradfri@0.3.0
│ ├── debug@3.1.0
│ └─┬ node-coap-client@0.4.4
│   ├── debug@3.1.0
│   └─┬ node-dtls-client@0.2.1
│     ├── debug@3.1.0
│     └─┬ node-aead-crypto@1.0.2
│       └─┬ node-pre-gyp@0.6.36
│         └─┬ tar-pack@3.4.0
│           └── debug@2.6.8

The first three (until dtls-client) are the ones I’m interested in. It should probably work then, since they are all @3.1.0. Or can the outdated version from tar-pack screw me here?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redirect debug output from required modules? #505 - GitHub
Hey all, I have a NodeJS-application in which I'm trying to redirect the debug output from modules I require.
Read more >
How to redirect output of one module only to a file?
In a module you want to redirect everything to stdout. import sys stdout_sav = sys.stdout fout = open('file.log', 'w') sys.stdout = fout ...
Read more >
Redirect output of debug command to copy.content module
Hello community,I want to Redirect output of debug command to copy.content module, to gather hosts facts and copy it locally inside a file ......
Read more >
Redirect debug output of bash script to file - blog of stigok
I needed a way to debug my thunar (file manager) custom actions, as it doesn't have a way of debugging these scripts itself....
Read more >
Capture all the logs - Gleb Bahmutov
Perfect - can we capture the log calls to the debug instances? Well, debug module writes directly into process.stderr stream bypassing console.* ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found