matrix-js-sdk icon indicating copy to clipboard operation
matrix-js-sdk copied to clipboard

Modules using a variable binding to `logger.log` do not respect `logger.setLevel`

Open davidisaaclee opened this issue 1 year ago • 2 comments

Issue

I'm getting noisy logs from this library despite setting loglevel.getLogger("matrix").disableAll().

Cause

https://github.com/matrix-org/matrix-js-sdk/blob/acbcb4658a5d5903dfd557e3e115241d0a6f38bb/src/models/event-timeline-set.ts#L30-L38 https://github.com/matrix-org/matrix-js-sdk/blob/acbcb4658a5d5903dfd557e3e115241d0a6f38bb/src/timeline-window.ts#L34

loglevel applies setLevel by replacing the relevant methods on the logger object: https://github.com/pimterry/loglevel/blob/f0187213feb6495630545a34a3b91633db47a1ee/lib/loglevel.js#L86-L97

If debuglog binds logger.log at the time of module load, debuglog will not be updated to respect setLevel. This causes noisy logs in my application even when running loglevel.getLogger("matrix").disableAll().

Possible fixes

  1. Use logger.log directly instead of debuglog. I don't understand why the library is disabling debuglog based on the local DEBUG constant, but I bet this could be achieved with loglevel's setLevel.
  2. Forfeit line numbers and use a reference to logger.log (e.g. const debuglog = (...args) => logger.log(...args))
  3. Some galaxy-brain Proxy implementation that I wasn't able to achieve (feel like there's a solution here that would keep the current DEBUG behavior and get line numbers with logs)

I've implemented option 1 in my branch: https://github.com/davidisaaclee/matrix-js-sdk/commit/9849818efa1d9101e68126af60fba180d7a5f756

Please let me know if there's a better way to get these changes moving than filing Github issues – it seems like you have a lot on your plate, and I'd like to help in an effective way.

davidisaaclee avatar Jun 28 '23 02:06 davidisaaclee

have you tried logger.disableAll()? seems to work here

vractal avatar Nov 01 '23 14:11 vractal

@vractal I haven't, but that would prevent me from using my own logger instances, right?

davidisaaclee avatar Nov 02 '23 18:11 davidisaaclee