All files / src/packages/logger/pino index.ts

50% Statements 6/12
27.27% Branches 3/11
14.28% Functions 1/7
50% Lines 6/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561x   1x   1x   1x         1x                                         1x                                              
import pino from 'pino';
import { IBaseLogger, ILogInput } from '../shared/interface';
import { DEFAULT_CONFIG, DEFAULT_REDACT_CONFIG, DEFAULT_TRANSPORT_CONFIG } from './const';
import { ILoggerConfig } from './interface';
import { getCommonData } from '../shared/utils';
 
export class PinoLogger implements IBaseLogger {
  private readonly pinoLogger: pino.Logger;
  private readonly config: ILoggerConfig;
 
  constructor(config?: ILoggerConfig) {
    this.config = {
      ...DEFAULT_CONFIG,
      ...config,
      ...((config?.redact?.enabled ?? DEFAULT_REDACT_CONFIG.enabled) && {
        redact: {
          ...DEFAULT_REDACT_CONFIG,
          ...config?.redact,
        },
      }),
      ...(config?.prettyEnabled && {
        transport: {
          ...DEFAULT_TRANSPORT_CONFIG,
          ...config?.transport,
          options: {
            ...DEFAULT_TRANSPORT_CONFIG.options,
            ...config?.transport?.options,
            ...(config?.colorEnabled && { colorize: config.colorEnabled }),
          },
        },
      }),
    };
    this.pinoLogger = pino(this.config);
  }
 
  private getCommonMessage(message: string, data?: ILogInput) {
    return data && this.config.prettyEnabled ? `${message} |` : message;
  }
 
  info(message: string, data?: ILogInput) {
    this.pinoLogger.info(getCommonData(data), this.getCommonMessage(message, data));
  }
  debug(message: string, data?: ILogInput) {
    this.pinoLogger.debug(getCommonData(data), this.getCommonMessage(message, data));
  }
  warn(message: string, data?: ILogInput) {
    this.pinoLogger.warn(getCommonData(data), this.getCommonMessage(message, data));
  }
  error(message: string, data?: ILogInput) {
    this.pinoLogger.error(getCommonData(data), this.getCommonMessage(message, data));
  }
  fatal(message: string, data?: ILogInput) {
    this.pinoLogger.fatal(getCommonData(data), this.getCommonMessage(message, data));
  }
}