import winston , { createLogger , format , transports } from 'winston' ; export class LoggerService { private readonly winstonLogger : winston . Logger ; private context : string ; private static instance : LoggerService ; constructor ( context ) { this . winstonLogger = LoggerService . createWinstonLogger ( ) ; this . context = context ; } param { string } context returns { LoggerService } public static getInstance ( context ) { LoggerService . instance = new LoggerService ( context ) ; return LoggerService . instance ; } private static createWinstonLogger ( ) { const logFormat = format . printf ( ( info ) => `${ info . level }: [${ info . timestamp }]${ info . message }` ) ; const transportList = [ new transports . Console ( { format : format . combine ( format . timestamp ( ) , format . colorize ( ) , logFormat ) , } ) , ] ; return createLogger ( { transports : transportList , exitOnError : false , } ) ; } private createLog ( level , message ) { const moduleName = this . context ; const messageString = typeof message === 'object' && message ? JSON . stringify ( message , null , 2 ) : message ; this . winstonLogger . log ( level , `[${ moduleName }] ${ messageString }` ) ; } param message debug ( message ) { this . createLog ( 'debug' , message ) ; } info ( message ) { this . createLog ( 'info' , message ) ; } error ( message ) { this . createLog ( 'error' , message ) ; } warn ( message ) { this . createLog ( 'warn' , message ) ; } }