All files / src/packages/logger/nest-logger index.ts

18.75% Statements 3/16
0% Branches 0/11
0% Functions 0/9
18.75% Lines 3/16

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 511x     1x   1x                                                                                          
import { Logger } from '@nestjs/common';
import { IBaseLogger, ILogInput } from '../shared/interface';
import { INestLoggerOptions } from './interface';
import { DEFAULT_REDACT_OPTIONS } from './const';
 
export class NestLogger implements IBaseLogger {
  private readonly logger: Logger;
  private readonly options: INestLoggerOptions;
 
  constructor(options?: INestLoggerOptions) {
    this.options = { ...options };
    this.logger = new Logger(NestLogger.name);
  }
 
  private censorData(data?: ILogInput) {
    const censorKeys = this.options?.redact?.paths || DEFAULT_REDACT_OPTIONS.paths;
    censorKeys.forEach((key) => {
      Iif (data && Object.keys(data).includes(key))
        data[key] = this.options?.redact?.censor ?? DEFAULT_REDACT_OPTIONS.censor;
    });
    return data;
  }
 
  private getCommonMessage(message: string, data?: ILogInput) {
    return [
      message,
      data
        ? JSON.stringify(this.options.redact?.enabled ? this.censorData(data) : data)
        : undefined,
    ]
      .filter(Boolean)
      .join(' | ');
  }
 
  info(message: string, data?: ILogInput) {
    this.logger.log(this.getCommonMessage(message, data));
  }
  debug(message: string, data?: ILogInput) {
    this.logger.debug(this.getCommonMessage(message, data));
  }
  warn(message: string, data?: ILogInput) {
    this.logger.warn(this.getCommonMessage(message, data));
  }
  error(message: string, data?: ILogInput) {
    this.logger.error(this.getCommonMessage(message, data));
  }
  fatal(message: string, data?: ILogInput) {
    this.logger.fatal(this.getCommonMessage(message, data));
  }
}