-
Notifications
You must be signed in to change notification settings - Fork 946
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
Debug mixed with console.log #952
Comments
I don't think this is possible because the problem is the terminal sees all the output (including stdout and stderr) mixed together, and it's the one responding to the colour codes. The only way would be to tell Debug to funnel all its output through the same process the rest of the code is using for coloured output, so that single output function can correctly push and pop the colour state. You can tell Debug to use a different output function (e.g. |
Thanks for the reply. Are you referring to the following section of the docs?
So I would do something like:
Still a bit confused by all this. I guess I would need to do this in each file I use debug? Also, surely there’s a reason debug has it’s own output function? Are there downsides to using console.log as the output function rather than the one provided by debug? |
No, both output to stdout. console.error goes to stderr. The reason why they mix is because stdout by default only emits when a newline is printed, whereas stderr emits immediately. Since things are emitted in chunks to avoid having to buffer things in memory (pretty common) you get "mixed" output. Note that this is only cosmetic. You can redirect streams. For example if you do
you'll see only the output from debug (assuming you haven't overrided the logger function). |
I think you can only solve this if you implement your own output function and have every part of your code use it.
This tells the Debug library to call
This function you write would then manage all the incoming requests for printing text from all parts of your codebase, and output it all in such a way that the colours don't mess up on the terminal. You just have to make sure everything that wants to print text goes via your function - the code above redirects Debug's output to your function, but you'd have to figure out how to do that for everything else your code wants to print too. Then "all" you have to do is implement A simpler solution might be to forget all this stuff for debugging, and just focus on the "real" output your program writes. If you buffer this real output (e.g. into a string) and then when you're done you print it in one single call to |
I’m not super concerned about the colors, it’s really a nice to have. What’s more important when I have DEBUG set, is that the console.log and debug output get to the screen in sync. If the colours disappear from program output when I turn debug on, that’s not the end of the world. Does that make it any easier? |
I endep if on this ticket while trying to redirect node output to a file and discovered that it is broken:
Apparently DEBUG_FD was removed from current versions of debug but node itself is still using the older ones as otherwise it would not get the error. Also trying to use the undocumented DEBUG_FILE did not had any effects. I still want to redirect only express logging to another file while keeping the stdout/stderr on console but this not seems to be possible. |
I use the debug module a lot. It’s great.
One thing I’m finding an issue though is with CLI apps, because these tend to use console.log with something like chalk for colors, and when you set your DEBUG string the output from console.log and debug get mixed together in a way that’s quite unreadable because all the outputs are out of sync.
Is there a way to ensure that output from debug and console .log stay in sync?
The text was updated successfully, but these errors were encountered: