unjs / consola

🐨 Elegant Console Logger for Node.js and Browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CONSOLA_LEVEL passed in the npm script command has no effect

gkatsanos opened this issue · comments

I use CONSOLA_LEVEL to try to have 2 commands in my package.json that show more or less logs depending on what kind of development I'm doing,

  "scripts": {
    "build": "nuxt build --modern=client",
    "build:analyze": "nuxt build -a",
    "dev": "CONSOLA_LEVEL=1 nuxt",
    "dev:debug": "CONSOLA_LEVEL=4 node --inspect node_modules/.bin/nuxt",

Unfortunately though the only thing I seem to be able to achieve is either hide all logs, or show all logs, without respecting the log level.

I believe this is because I use a custom reporter,

  consola.setReporters([
    {
      log: (e) => {
        process.stdout.write(`${JSON.stringify(...e.args)}\n`)
      },
    },
  ])

and because I don't log with consola but with console:

export default (_, inject) => {
  function logger(level, payload, message) {
    const span = tracer.scope().active()
    const time = new Date().toISOString()
    const record = { time, level, ...payload, message }

    if (span) {
      tracer.inject(span.context(), formats.LOG, record)
    }

    console.log(record)
  }

  inject('logger', logger)
}

(I need this for our production monitoring/logging system (datadog))

So I changed the setReporters above and did this instead:

if (isProd) {
  consola.setReporters([
    {
      log: (e) => {
        process.stdout.write(`${JSON.stringify(...e.args)}\n`)
      },
    },
  ])
} else {
  consola.wrapAll()
}

but unfortunately when you do console.log , even when you pass a level field, it's not passed to consola as a loglevel. Any idea how to make this work? (we need a centralized middlerware/pluging around all our logs)