Custom Logger

Introduction

Logging to a file can be done utilizing a module like winston. This can be obtained through using the verbose renderer and creating a custom logger class that implements Logger which is exported from the index.

While calling a new Listr you can call it with { renderer: 'verbose', rendererOptions: { logger: MyLoggerClass } }.

import { logLevels, Logger, LoggerOptions } from 'listr2'

export class MyLoggerClass extends Logger {
  constructor(private options?: LoggerOptions) {
    // This is not needed if you don't use these options in your custom logger
  }

  /* CUSTOM LOGIC */
  /* CUSTOM LOGIC */
  /* CUSTOM LOGIC */
}

Options

This is related to the discussions on issue #561: "fix: pass options to custom logger".


Thanks marcelmeulemans for bringing this up!

You can also pass options to your custom logger via renderer options.

{ renderer: 'verbose', rendererOptions: { logger: MyLoggerClass, options: { file: '/var/log/something.txt' } } }
Edit this page on GitHub Updated at Tue, Feb 1, 2022