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

Programmatic log levels #164

Closed eurostar-fennec-cooper closed 3 years ago

eurostar-fennec-cooper commented 3 years ago

Suggestion: in many loggers, there is a method where one can pass the log level, followed by the log messages. It would be very useful for loglevel to support this as well. Ie

loglevel.log(level, ...messages)

Versus

loglevel[level](...messages)
pimterry commented 3 years ago

Unfortunately, if you do that then you will break the stack trace in the browser console. Instead of coming from the right place inside your code, every message will come from the same line, inside your log function.

Currently, loglevel avoids this by ensuring that every logging function is just a bound version of the appropriate console function (or a no-op function, if the log level is disabled). That means there's never any custom logic in between your code and the console logging code, so no noise in the stacktrace.

Because of that I'm not keen to include this as a built-in feature of loglevel. It's easy to enable it yourself if you need it regardless though:

loglevel.log = function (level, ...messages) {
  loglevel[level](...messages);
}

I'm going to close this for now. If you find any way to implement this without breaking stack traces, I'd be very interested, but otherwise I think it's best implemented as an external 'plugin', as above.