debug-js / debug

A tiny JavaScript debugging utility modelled after Node.js core's debugging technique. Works in Node.js and web browsers
MIT License
11.1k stars 927 forks source link

Can't set global `log()` function overriding namespaces ones #873

Open piranna opened 2 years ago

piranna commented 2 years ago

On the example located at https://github.com/debug-js/debug#output-streams, it shows that's possible to set custom log() functions for each namespace, but also that setting it directly on the debug module takes precedence and will be used instead of the namespace ones. This is specially useful in my use case, since I want to capture Mediasoup debuging Logger and redirect it to Moleculer logger. Problem is that according to https://github.com/debug-js/debug/blob/c0805cc4d3b2c7c987567e99ecaec78840516d7b/src/common.js#L112-L113 namespaces log() function has absolute priority, so it's not possible to do it globally, and it's not possible to directly bind on top of Mediasoup Loggers since they are private, so I'm in an end road.

Since the linken example at https://github.com/debug-js/debug/blob/master/examples/node/stdout.js doesn't exists too, I'm not sure if the documentation is outdated or wrong, or if setting debug.log() should effectively override per-namespace log() function and this is in fact a bug. Besides fixing this one way or another, how can I solve my problem of redirect Mediasoup log messages to Moleculer?

Qix- commented 2 years ago

Yes but self.log shouldn't be set to anything.

Welcome to Node.js v17.3.1.
Type ".help" for more information.
> const debug = require('debug');
undefined
> debug.log
[Function: log]
> d1 = debug('foo:bar')
[Function: debug] {
  namespace: 'foo:bar',
  useColors: true,
  color: 4,
  extend: [Function: extend],
  destroy: [Function: deprecated],
  enabled: [Getter/Setter],
  inspectOpts: {}
}
> d1.log
undefined
> d1.enabled = true
true
> d1('test')
  foo:bar test +0ms
undefined
> debug.log = (...args) => console.error('MY DEBUG:', ...args);
[Function (anonymous)]
> d1('test')
MY DEBUG:   foo:bar test +24s
undefined

This works for me on the latest version. Can you reproduce that locally?

piranna commented 2 years ago

This works for me on the latest version.

It's not the same case here, that works because d1.log is undefined, but at Mediasoup https://github.com/versatica/mediasoup/blob/4783b3dde93d43b5b120a8088af873aa5661202f/node/src/Logger.ts#L27-L29 d1.log is set to a function, the same as what the example in the docs says: https://github.com/debug-js/debug/blob/c0805cc4d3b2c7c987567e99ecaec78840516d7b/README.md?plain=1#L254-L272

With d1.log set to a function as debug documentation example does (and also Mediasoup does too), it fails.

piranna commented 2 years ago

How can I help you with that? It's tagged as question, but it's also a bug on docs and/or actual implementation. How can we move this forward?

Qix- commented 2 years ago

I'm not sure what you want. If you specify a logger on individual debug instances, of course it will override the global logger. That's well defined.

What behavior would you like to see happen?

piranna commented 2 years ago

What behavior would you like to see happen?

According to the docs example, if you define a logger in debug instance, it will override all the ones from its parent debug instances, but if you define a global logger, it will override all the debug instances. The example in the docs is very explicit about this behaviour:

https://github.com/debug-js/debug/blob/c0805cc4d3b2c7c987567e99ecaec78840516d7b/README.md?plain=1#L254-L272

Qix- commented 2 years ago

You're right, GitHub has a scrollbar on mobile and I missed it in your other comment. My apologies.

PR welcome to fix the docs. There isn't a great way to fix this that won't break a bunch of people unfortunately. I need to think about how to approach this for v5.

piranna commented 2 years ago

Thing is, it seems like this was the previous behaviour, but can't find the actual reference.

shryao1 commented 2 years ago

This is interesting

RabiaSaid commented 2 years ago

2.6.8

RabiaSaid commented 2 years ago

4.3.4