unjs / consola

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

Please expose the builtin reporter #181

Closed liuweiGL closed 1 year ago

liuweiGL commented 1 year ago

Describe the feature

Before version 3.0, I could easily implement my own reporter by inheriting from BasicReporter or FancyReporter

Just like:

export type FileReporterProps = BasicReporterOptions & {
  outDir?: string
}

const DEFAULT_OUT_DIR = path.resolve(WORKSPACE, 'logs')

export class FileReporter extends BasicReporter {
  private outDir: string
  constructor({
    outDir = DEFAULT_OUT_DIR,
    dateFormat = 'YYYY-MM-DD HH:mm:ss.SSS',
    ...basicProps
  }: FileReporterProps = {}) {
    super({ dateFormat, ...basicProps })

    this.outDir = outDir
  }

  private getStream(name: string, flags = 'a+') {
    const file = path.resolve(this.outDir, `${snakeCase(name)}.log`)
    ensureExists(file)
    return createWriteStream(file, { flags, autoClose: true })
  }

  protected override formatLogObj(logObj: ConsolaReporterLogObject): string {
    return `[${this.formatDate(new Date())}] ${super.formatLogObj(logObj)}\n`
  }

  public override log(
    logObj: ConsolaReporterLogObject,
    { stderr }: ConsolaReporterArgs
  ): void {
    if ((logObj as any).persistent !== true) {
      return
    }

    const line = this.formatLogObj(logObj)
    const stream = this.getStream(logObj.tag || 'default')

    stream.write(line, err => {
      if (err) {
        stderr.write(err.message)
      }
    })
  }
}

or

const LEVEL_COLOR_MAP: Record<LogLevel, Chalk> = {
  0: chalk.bgRed,
  1: chalk.bgYellow,
  2: chalk.bgBlue,
  3: chalk.bgGreen,
  4: chalk.bgWhite,
  5: chalk.bgGray,
  [LogLevel.Silent]: chalk.bgGray,
  [LogLevel.Verbose]: chalk.bgGray
}

export class PrettyReporter extends FancyReporter {
  constructor(props: FancyReporterOptions) {
    super(props)
  }

  protected override formatType(logObj: ConsolaReporterLogObject) {
    return LEVEL_COLOR_MAP[logObj.level].black(` ${logObj.type.toUpperCase()} `)
  }

  protected override formatLogObj(
    logObj: ConsolaReporterLogObject,
    options?: any
  ) {
    (logObj as any).badge = true
    if (logObj.level === LogLevel.Success) {
      logObj.args[0] = chalk.green(logObj.args[0])
    } else if (logObj.level === LogLevel.Error) {
      logObj.args[0] = chalk.red(logObj.args[0])
    } else if (logObj.level === LogLevel.Warn) {
      logObj.args[0] = chalk.yellow(logObj.args[0])
    } else if (logObj.level === LogLevel.Log) {
      logObj.args[0] = chalk.blue(logObj.args[0])
    }
    // @ts-ignore
    return super.formatLogObj(logObj, options)
  }
}

Additional information

pi0 commented 1 year ago

Hi dear @liuweiGL. I undrestand that you need same customization possibilities as you had with v2 for fancy reporter. To make it easier for future improvements and internal refactors and also making customization easier, reporters are not exported anymore but you have the ability to use global consola options to modify their behavior (such as colors, stream, etc). What are the things you need to customize and not possible with v3?

liuweiGL commented 1 year ago

Just for the convenience of upgrading to v3

liuweiGL commented 1 year ago

I respect the author's design, and I plan to refactor it some time later

mashirozx commented 1 year ago

Hi dear @liuweiGL. I undrestand that you need same customization possibilities as you had with v2 for fancy reporter. To make it easier for future improvements and internal refactors and also making customization easier, reporters are not exported anymore but you have the ability to use global consola options to modify their behavior (such as colors, stream, etc). What are the things you need to customize and not possible with v3?

In my condition, I need to hide the full stack trace info in consola.warn, but seems it's inconvenient to override the formatStack method

NozomuIkuta commented 1 year ago

The comment above is about #194.