All files / helpers log.helper.ts

87.5% Statements 7/8
66.66% Branches 2/3
66.66% Functions 2/3
87.5% Lines 7/8

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                          1x 1x                 1x               1x   1x           1x   1x      
import winston from "winston";
import { LOG_COLORS, LOG_LEVELS } from "../constants";
 
/**
 * A helper class for logging related operations.
 */
export abstract class LogHelper {
  /**
   * Retrieves the log level based on the current environment.
   *
   * @returns The log level as a string. In development mode, it returns "debug". In production mode, it returns "warn".
   */
  public static getLogLevel(environment: string = "development"): string {
    const isDevelopment = environment === "development";
    return isDevelopment ? "debug" : "warn";
  }
 
  /**
   * Creates and configures a winston logger instance with the specified log level, format, and transports.
   *
   * @returns A configured winston Logger instance.
   */
  public static getLogger(environment?: string): winston.Logger {
    const format = winston.format.combine(
      winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss:ms" }),
      winston.format.colorize({ all: true }),
      winston.format.printf(
        (info) => `${info.timestamp} ${info.level}: ${info.message}`
      )
    );
 
    const transports = [new winston.transports.Console()];
 
    const Logger = winston.createLogger({
      level: LogHelper.getLogLevel(environment),
      levels: LOG_LEVELS,
      format,
      transports,
    });
    winston.addColors(LOG_COLORS);
 
    return Logger;
  }
}