logger.ts 2.83 KB
Newer Older
1 2 3 4
import pino, { LoggerOptions as PinoLoggerOptions } from 'pino'
import pinoms, { Streams } from 'pino-multi-stream'
import { createWriteStream } from 'pino-sentry'
import { NodeOptions } from '@sentry/node'
5

6 7 8 9 10 11 12 13 14
export const logLevels = [
  'trace',
  'debug',
  'info',
  'warn',
  'error',
  'fatal',
] as const
export type LogLevel = typeof logLevels[number]
15 16 17 18

export interface LoggerOptions {
  name: string
  level?: LogLevel
19 20
  sentryOptions?: NodeOptions
  streams?: Streams
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
}

/**
 * Temporary wrapper class to maintain earlier module interface.
 */
export class Logger {
  options: LoggerOptions
  inner: pino.Logger

  constructor(options: LoggerOptions) {
    this.options = options

    const loggerOptions: PinoLoggerOptions = {
      name: options.name,

      level: options.level || 'debug',

      // Remove pid and hostname considering production runs inside docker
      base: null,
    }

42 43 44 45
    let loggerStreams: Streams = [{ stream: process.stdout }]
    if (options.sentryOptions) {
      loggerStreams.push({
        level: 'error',
46 47 48 49
        stream: createWriteStream({
          ...options.sentryOptions,
          stackAttributeKey: 'err',
        }),
50 51
      })
    }
52 53 54
    if (options.streams) {
      loggerStreams = loggerStreams.concat(options.streams)
    }
55 56

    this.inner = pino(loggerOptions, pinoms.multistream(loggerStreams))
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  }

  child(bindings: pino.Bindings): Logger {
    const inner = this.inner.child(bindings)
    const logger = new Logger(this.options)
    logger.inner = inner
    return logger
  }

  trace(msg: string, o?: object, ...args: any[]): void {
    if (o) {
      this.inner.trace(o, msg, ...args)
    } else {
      this.inner.trace(msg, ...args)
    }
  }

  debug(msg: string, o?: object, ...args: any[]): void {
    if (o) {
      this.inner.debug(o, msg, ...args)
    } else {
      this.inner.debug(msg, ...args)
    }
  }

  info(msg: string, o?: object, ...args: any[]): void {
    if (o) {
      this.inner.info(o, msg, ...args)
    } else {
      this.inner.info(msg, ...args)
    }
  }

  warn(msg: string, o?: object, ...args: any[]): void {
    if (o) {
      this.inner.warn(o, msg, ...args)
    } else {
      this.inner.warn(msg, ...args)
    }
  }

  warning(msg: string, o?: object, ...args: any[]): void {
    if (o) {
      this.inner.warn(o, msg, ...args)
    } else {
      this.inner.warn(msg, ...args)
    }
  }

  error(msg: string, o?: object, ...args: any[]): void {
    if (o) {
108 109 110 111 112
      // Formatting error log for Sentry
      const context = {
        extra: { ...o },
      }
      this.inner.error(context, msg, ...args)
113 114 115 116 117 118 119
    } else {
      this.inner.error(msg, ...args)
    }
  }

  fatal(msg: string, o?: object, ...args: any[]): void {
    if (o) {
120 121 122 123
      const context = {
        extra: { ...o },
      }
      this.inner.fatal(context, msg, ...args)
124 125 126 127 128
    } else {
      this.inner.fatal(msg, ...args)
    }
  }
}