dahlia / logtape

Simple logging library with zero dependencies for Deno, Node.js, Bun, browsers, and edge functions
http://logtape.org/
MIT License
520 stars 11 forks source link

Built-in text formatters should be configurable #13

Closed ryuapp closed 1 month ago

ryuapp commented 2 months ago

I think this opinion is just a preference. Log levels are easier to understand with formal names.

DBG -> DEBUG
INF -> INFO
WRN -> WARNING
ERR -> ERROR
FTL -> FATAL

Is it possible to provide an option that does not use abbreviations?

dahlia commented 2 months ago

You can customize the text formatter of the sinks. However, it's not easy to just change the labels for log levels. The current workaround is like:

const sink = getConsoleSink({
  formatter(record: string) {
    const ts = new Date(record.timestamp);
    let msg = "";
    for (let i = 0; i < record.message.length; i++) {
      if (i % 2 === 0) msg += record.message[i];
      else msg += inspect(record.message[i]);
    }
    const category = record.category.join("\xb7");
    return `${ts.toISOString().replace("T", " ").replace("Z", " +00:00")} [${
      record.level.toUpperCase()
    }] ${category}: ${msg}\n`;
  },
});

I'm giving the default text formatter more flexibility in the next release, e.g.:

const sink = getConsoleSink({
  formatter: getDefaultFormatter({
    logLevel(level: LogLevel) {
      return level.toUpperCase();
    }
  }),
});
ryuapp commented 2 months ago

Your suggestion meets my requirements. I look forward to the next release. Thank you.

dahlia commented 1 month ago

You can now customize each part of log messages. It's available for preview in v0.6.0-dev.74+f4e8f8e6 (JSR & npm).

dahlia commented 1 month ago

This is shipped with LogTape v0.6.0 (JSR & npm).