loglevel: Missing 'log' method to serve as a drop-in replacement for 'console'
I’d like to replace all console.*
calls to loglevel.*
calls but I discovered that there is no log
method which is widely used with console
. The info
method does not fit well because the browser console highlights the info message with an icon making these messages somewhat more specific than default logging.
Any chance adding log
?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 1
- Comments: 19 (5 by maintainers)
Commits related to this issue
- Map log.debug to console.log, as debug seems to have been deprecated Chrome now no longer prints console.debug messages, and they're now a noop. This change transparently maps all debug logging to co... — committed to pimterry/loglevel by pimterry 7 years ago
- Add an alias `log` for `debug`, for easier migration from console.log. Connects-To: #64 — committed to pimterry/loglevel by pimterry 7 years ago
I’ll throw my two cents into the mix:
Problems
console.trace
sucks for trace level loggingI avoid using
console.trace
unless I want to actually see a stack trace. This is because Chrome shows the stack trace expanded, which clutters up the console. I don’t feel it is a good method to use for the trace level of a logging tool.However, by not using
log.trace
withloglevel
, I’ve limited myself to only 4 levels (error
,warn
,info
, anddebug
). This is annoying because I often would like to addtrace
level logs for things that are deeper thandebug
. But I do not want to see stack traces for them.Note that
console.warn
andconsole.error
in Chrome also include stack traces, but they are much more usable since the traces are collapsed. I only have an issue withconsole.trace
.Not taking full advantage of browser log styling
It is important to consider that Chrome styles
console.log
,console.debug
,console.info
,console.warn
, andconsole.error
differently. However,loglevel
doesn’t take full advantage of this by not supportingconsole.log
. IMO, this is a major downside ofloglevel
at this time.Making
log.log
an alias for some other loglevel will not solve this. We really need a new loglevel forconsole.log
.App environment is affected by
loglevel
I agree with @sompylasar that many app developers don’t want their logging tool to impact the app environment without permission. However, the local storage feature is useful and think it makes sense to continue to support it.
Proposed Solution
I propose the following enhancements be made to
loglevel
:verbose
log level betweendebug
andtrace
log.verbose
method that maps toconsole.log
log.log
method that maps toconsole.log
I disagree with @mroderick regarding solving this by decorating log methods or creating a plugin. As soon as any wrapper is used around
loglevel
, the browser will report logs as coming from the wrapping code’s file and line number instead of the actual file and line of thelog
call within your application.I am willing to make these improvements and create a PR, but I’d like to hear from @pimterry and others that this proposal would be acceptable.
I use the latest Google Chrome, the devtools console renders
debug
in blue color,log
in black color,info
in black color with a blue info icon on the left.I needed a tiny library that copes with console inconsistencies and is browserifiable/webpackable. For my case I forked and patched
loglevel
to remove persistence and addlog
as a second method on theINFO
level (see my fork: https://github.com/sompylasar/loglevel/blob/master/lib/loglevel.js#L77 ). I am not suggesting to use the same level for all logging, I am suggesting to wrap every logging method the browser provides.I understand your thoughts on persistence: you want to visit your app in production, tamper with your own storage and read the logs. It’s a good idea. But if the persistence is hardcoded into the logging library, it touches the environment without express permission from the app author. I see this as putting the persistence part into a separate optional module which lets configure the cookie and the storage or opt out of persistence, and using
loglevel.setLevel(loglevelPersistence.getLogLevel())
once in the app’s entry point. No further code changes are needed.I’d rather not have a
log.log
method. As in my first posts way above, it’s not a common log level in most other libraries, it’s not obvious where it fits in the semantic order of levels, and it would be a breaking change anyway, which I’d really like to avoid.I do totally agree we need to find a solution now that
console.debug
seems to have been quietly killed though. I need to think about it further, but in principle I think I’d be happy to makelog.debug
start usingconsole.log
under the hood, instead ofconsole.debug
. How would people feel about that?No, it does not. The browser console outputs the
log
anddebug
messages differently.This seems like a good solution to me as well. Because
console.trace
has a different behavior than the other log methods, I personally don’t use it as “standard” loglevel in my apps. And withconsole.debug
being a noop, the log methods available for my apps has shrunk to justlog.info
,log.warn
, andlog.error
. Makinglog.debug
work again by mapping toconsole.log
would certainly help!Why not just have
log.log
be an alias forlog.debug
? It wouldn’t add another loglevel, but it would be available for consistency for those who wish their logging tool to have the same API as browser logging. I personally would uselog.debug
overlog.log
, but I wouldn’t mind the alias existing. And doing this would squash many of the complaints people have.Revised Proposal
Now that
console.debug
is no longer a thing, I would revise my proposal (see post above) to the following:log.debug
method to map toconsole.log
log.log
method that maps toconsole.log
(as an alias, no additional loglevel)I’ve removed
log.verbose
because there isn’t anything for it to map to.I’d like to +1 adding a “log” method. I just ran into this same issue while integrating loglevel. It’s such a common method that it needs to be in the lib; I’d dare say that it’s more often used the “info”.