unjs / consola

🐨 Elegant Console Logger for Node.js and Browser
Other
6.12k stars 175 forks source link

Support Datadog log level in JSONReporter #162

Closed nicolaspayot closed 1 year ago

nicolaspayot commented 1 year ago

Describe the feature

Hi! We're using consola in our Nuxt 3 applications and send logs to Datadog.

When using the JSONReporter, json logs have a level property that maps to a number (enum LogLevel) and a type property that maps to a string (type logType).

It seems that Datadog uses the level property to classify the logs between error, warn, log, etc, but only when it's mapped to string types such as error, warn, log, etc. It's not working when using numbers and most logs are interpreted as error.

I've implemented a custom JSONReporter to use the level property as a string:

log(logObj: ConsolaReporterLogObject) {
    const newLogObj = {
        level: logObject.type,
        tag: logObject.tag,
        args: logObject.args,
        date: logObject.date,
    }
    this.stream.write(JSON.stringify(newLogObj) + '\n');
}

Would that make sense for the built-in JSONReporter to support this feature out of the box?

Additional information

pi0 commented 1 year ago

Hi and sorry for late response. Consola v3 has no built-in json reporter. You can implement one for your self with expected JSON format.