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

methodFactory logLevel parametr has always same value #166

Closed xmedeko closed 3 years ago

xmedeko commented 3 years ago

When I set my own loglevel.methodFactory then the parameter has always same value - the min. number set by setLevel. E.g. https://jsfiddle.net/xmedeko/e9z1038x/9/ :

var originalFactory = log.methodFactory;
log.methodFactory = function (methodName, logLevel, loggerName) {
  var rawMethod = originalFactory(methodName, logLevel, loggerName);
  return function (...args) {
    rawMethod(methodName, logLevel, ...args);
  }
}

log.setLevel(1);
log.info("info message");
log.warn("warn message");
log.error("error message");

The output shows logLevel param as 1, 1, 1. Is this behaviour ok?

I would expect it to be 2, 3, 4 according to the methodName. So as I can do e.g.

log.methodFactory = function (methodName, logLevel, loggerName) {
  var rawMethod = originalFactory(methodName, logLevel, loggerName);
  return function (...args) {
    rawMethod(...args);
    if (logLevel >= 3) sendLogSomewhere(...);
  }
}
pimterry commented 3 years ago

Yes, that's correct! It's telling you the configured loglevel, not the level of the logger you're implementing (which would really just duplicate methodName).

This argument is provided primarily really because it's needed for the internal method factory implementation that uses it as part of supporting older IE versions which was important when this API was created (more than 7 years ago now!). At this stage it'd be very difficult to change without breaking existing plugins, and I can imagine there might be cases where it's useful to know the overall loglevel like this anyway.

Is this causing you problems? You should be able to get the level for the current method as a number by combining methodName and loglevel.LEVELS I think.

If you think the documentation could be improved here, I'm open to PR to clarify that.

xmedeko commented 3 years ago

Yes, the current log level may be gotten from methodName easily, it was just confusing for me. I will try to make doc PR.