Source

logger.ts

/* eslint-disable no-console */
export interface LogFn {
  (contextObj: unknown, msg?: string, ...interpolationArgs: unknown[]): void;
  (msg: string, ...interpolationArgs: unknown[]): void;
}

export const ConsoleLogger: ILogger = {
  fatal: console.error,
  error: console.error,
  warn: console.warn,
  info: console.info,
  debug: console.debug,
  trace: console.debug,
};

/**
 * A generic logging interface modeled after the Pino logger.
 * If your preferred logger does not match this interface you can create a
 * wrapper that does.
 */
export interface ILogger {
  /**
   * Log at `'fatal'` level the given msg.
   * @function
   * @param obj an object containing additional context about the log message.
   * The intention is for your logger to output this context as additional
   * metadata accompanying the log message.
   * @param msg the log message to write
   * @param ...interpolationArgs all arguments supplied after message are
   * intended for serialization and interpolation into `msg`, using any
   * printf-style placeholders it has, e.g. (%s, %d, %o|%O|%j)
   */
  fatal: LogFn;

  /**
   * Log at `'error'` level the given msg.
   * @function
   * @param obj an object containing additional context about the log message.
   * The intention is for your logger to output this context as additional
   * metadata accompanying the log message.
   * @param msg the log message to write
   * @param ...interpolationArgs all arguments supplied after message are
   * intended for serialization and interpolation into `msg`, using any
   * printf-style placeholders it has, e.g. (%s, %d, %o|%O|%j)
   */
  error: LogFn;

  /**
   * Log at `'warn'` level the given msg.
   * @function
   * @param obj an object containing additional context about the log message.
   * The intention is for your logger to output this context as additional
   * metadata accompanying the log message.
   * @param msg the log message to write
   * @param ...interpolationArgs all arguments supplied after message are
   * intended for serialization and interpolation into `msg`, using any
   * printf-style placeholders it has, e.g. (%s, %d, %o|%O|%j)
   */
  warn: LogFn;

  /**
   * Log at `'info'` level the given msg.
   * @function
   * @param obj an object containing additional context about the log message.
   * The intention is for your logger to output this context as additional
   * metadata accompanying the log message.
   * @param msg the log message to write
   * @param ...interpolationArgs all arguments supplied after message are
   * intended for serialization and interpolation into `msg`, using any
   * printf-style placeholders it has, e.g. (%s, %d, %o|%O|%j)
   */
  info: LogFn;

  /**
   * Log at `'debug'` level the given msg.
   * @function
   * @param obj an object containing additional context about the log message.
   * The intention is for your logger to output this context as additional
   * metadata accompanying the log message.
   * @param msg the log message to write
   * @param ...interpolationArgs all arguments supplied after message are
   * intended for serialization and interpolation into `msg`, using any
   * printf-style placeholders it has, e.g. (%s, %d, %o|%O|%j)
   */
  debug: LogFn;

  /**
   * Log at `'trace'` level the given msg.
   * @function
   * @param obj an object containing additional context about the log message.
   * The intention is for your logger to output this context as additional
   * metadata accompanying the log message.
   * @param msg the log message to write
   * @param ...interpolationArgs all arguments supplied after message are
   * intended for serialization and interpolation into `msg`, using any
   * printf-style placeholders it has, e.g. (%s, %d, %o|%O|%j)
   */
  trace: LogFn;
}