pimterry / loglevel

:ledger: Minimal lightweight logging for JavaScript, adding reliable log level methods to wrap any available console.log methods
MIT License
2.62k stars 157 forks source link

Reference change in log.debug? #129

Closed KonradHoeffner closed 5 years ago

KonradHoeffner commented 5 years ago

We want to encapsulate log.debug with additional functionality, but for the reference seems to change, which breaks our code:

const ref = log.debug;
console.log(ref===log.debug); // true
log.debug("Hello World");
// some more logging
console.log(ref===log.debug); // false

Is this intended functionality and a wrong assumption to make that the reference stays the same? Or must there be some other issue in our code?

We are using loglevel 1.6.1 in the browser.

pimterry commented 5 years ago

Can you put together a standalone reproduction so I can see this in action? It'd also be useful if you could be more specific about which browser you're using when you see this - a https://www.whatsmybrowser.org/ link would be perfect.

For now, using just the code above I can't seem to reproduce this. Skimming the code, the only time that calling log.debug would ever change the definition of log.debug is if console wasn't defined initially. That's designed to handle issues in IE (either old IE, or newer IE with compatibility modes set badly), and if you're not using IE I don't think this should ever happen.

One way you could see this effect is if you're calling setLevel between the two checks. Calling setLevel does redefine every log method, either creating it afresh or replacing it with a no-op, depending on the level selected.

KonradHoeffner commented 5 years ago

The browser is Firefox 67, the link is www.whatsmybrowser.org/b/S9N79PS. We do indeed use setLevel, I will investigate whether this solves the problem.

KonradHoeffner commented 5 years ago

Yes, setLevel was indeed the problem, that solved it, thanks!

pimterry commented 5 years ago

Great to hear, glad I could help! :+1: