consola v3 proposal

pi0
23.6k
pi0
commented 3 months ago
  • Use Proxy instead of creating closures for each log type. This allows using arbitrary log types and less overhead of tagged loggers and easier mocking
  • Find a way to make tagged loggers more lightweight when using withTag
  • Deprecate withDefaults and create (Keep for legacy but create a new class instance`
  • Support logs with timer (Using a token?)
  • Seperate <type>(logOjb) from <type>(args. (How?)
0
manniL
4.6k
manniL
commented 3 months ago

Translating error codes (as discussed in https://github.com/nuxt/nuxt.js/pull/4428) ๐Ÿ‘
(Or do you think this will land in 2.x ?)

0
pimlie
838
pimlie
commented 18 days ago

I was thinking about the Proxy/closure issue and actually wonder why we cant just use a normal function assignment instead of the _warpFn method with a .bind(this)? Like:

      const defaults = Object.assign(
        { type },
        this._types[type],
        this._defaults
      )

      const self = this
      this[type] = function _warpFn() {
        if (paused) {
          queue.push([this, defaults, arguments])
          return
        }

        self._logFn(defaults, arguments)
      }

In the past (consola v1?) we needed the .bind(this) because wrapFn was called createLogFn which contained an inner function log which had all the logging logic. But now that inner log function is just a class member _log so the above should also just work? (I did a quick test and it indeed seems to work). Although Proxy's might be nicer the above works a bit easier.

But the reason I was thinking about consola was that I implemented the same trick in nuxt-generate-cluster as you have added in nuxt-cli for exiting when a consola.fatal message is logged. Although adding the reporter works fine, maybe it would be nice if consola could also fire an event and we can just listen for that.

Something like:

export default class Consola extends EventEmitter { // <- not sure how this works in a browser
  ...
  for (const type in this._types) {
    ...
    this[type] = function _warpFn() {
      if (this._types[type].emitEvent) {
        ...
        this.emit(type, ...arguments)
        ...
    }
  }
}

and to use this we'd do

consola.on('fatal', (message) => {
   process.exit(1)
})

A side-effect of this is that this way we could emit the event before any reporter is called and just exit before calling the default reporters. Currently the fatal error is first displayed by the default Basic/FancyReporter and only after that the custom reporter is called (to call process.exit)

0
Informations
Question โ€ข Unresolved
#c39 - Created 3 months ago