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.tracesucks for trace level loggingI avoid using
console.traceunless 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.tracewithloglevel, I’ve limited myself to only 4 levels (error,warn,info, anddebug). This is annoying because I often would like to addtracelevel logs for things that are deeper thandebug. But I do not want to see stack traces for them.Note that
console.warnandconsole.errorin 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.errordifferently. However,logleveldoesn’t take full advantage of this by not supportingconsole.log. IMO, this is a major downside ofloglevelat this time.Making
log.logan alias for some other loglevel will not solve this. We really need a new loglevel forconsole.log.App environment is affected by
loglevelI 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:verboselog level betweendebugandtracelog.verbosemethod that maps toconsole.loglog.logmethod that maps toconsole.logI 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 thelogcall 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
debugin blue color,login black color,infoin 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
loglevelto remove persistence and addlogas a second method on theINFOlevel (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.logmethod. 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.debugseems to have been quietly killed though. I need to think about it further, but in principle I think I’d be happy to makelog.debugstart usingconsole.logunder the hood, instead ofconsole.debug. How would people feel about that?No, it does not. The browser console outputs the
loganddebugmessages differently.This seems like a good solution to me as well. Because
console.tracehas a different behavior than the other log methods, I personally don’t use it as “standard” loglevel in my apps. And withconsole.debugbeing a noop, the log methods available for my apps has shrunk to justlog.info,log.warn, andlog.error. Makinglog.debugwork again by mapping toconsole.logwould certainly help!Why not just have
log.logbe 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.debugoverlog.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.debugis no longer a thing, I would revise my proposal (see post above) to the following:log.debugmethod to map toconsole.loglog.logmethod that maps toconsole.log(as an alias, no additional loglevel)I’ve removed
log.verbosebecause 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”.