unjs / consola

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

add additional transports or export reporters #287

Open max-schu opened 2 months ago

max-schu commented 2 months ago

Describe the feature

In some cases we want consola to not log to the console, but send the formatted log to something else like a blessed instance. In my understanding this is currently only possible, through custom reporters. It would be great if we could set the transport target, or be able to extend existing basic and fancy reporters.

Additional information

pi0 commented 2 months ago

Can you explain with a sample code how you expect to transport (ANSI) formatted logs in an alternative way to reporters?

Normally you probably expect consistent logs and since reporters are pluggable it is almost impossible without a custom reporter for transport that formats exactly as you expect.

max-schu commented 2 months ago

ideally either an option to get access to the formatted log like so:

const logger = createConsola({
  transport: (log) => {
    // where log is the output of the used reporter
    // then we can use the formatted output
    someFunction(log)
  }
})

or and option to extend built in reporters like so:

import { createConsola, FancyReporter } from "consola";

const fancyReporter = new FancyReporter();

const consola = createConsola({
  reporters: [
    {
      log: (logObj) => {
        const formattedLog = fancyReporter(logObj);
        // then we can use the formatted output
        someFunction(log);
      },
    },
  ],
});

I might not have a perfect understanding on how everything works, so correct me if i'm overseeing something. what do you think about this?