import chalk from 'chalk' ; import { MpcastLogger , Logger , LogLevel } from './mpcast-logger' ; const DEFAULT_CONTEXT = 'Podcast Server' ; description example docsCategory export class DefaultLogger implements MpcastLogger { internal level : LogLevel = LogLevel . Info ; private readonly timestamp : boolean ; private readonly localeStringOptions = { year : '2-digit' , hour : 'numeric' , minute : 'numeric' , day : 'numeric' , month : 'numeric' , } ; private static originalLogLevel : LogLevel ; constructor ( options ? ) { this . level = options && options . level || LogLevel . Info ; this . timestamp = options && options . timestamp !== undefined ? options . timestamp : true ; } description internal static hideNestBoostrapLogs ( ) { const { logger } = Logger ; if ( logger instanceof DefaultLogger ) { if ( logger . level === LogLevel . Info ) { this . originalLogLevel = LogLevel . Info ; logger . level = LogLevel . Warn ; } } } description internal static restoreOriginalLogLevel ( ) { const { logger } = Logger ; if ( logger instanceof DefaultLogger && DefaultLogger . originalLogLevel !== undefined ) { logger . level = DefaultLogger . originalLogLevel ; } } error ( message , context ? , trace ? ) { if ( this . level >= LogLevel . Error ) { this . logMessage ( chalk . red ( `error` ) , chalk . red ( message + ( trace ? ` trace: \n${ trace }` : '' ) ) , context ) ; } } warn ( message , context ? ) { if ( this . level >= LogLevel . Warn ) { this . logMessage ( chalk . yellow ( `warn` ) , chalk . yellow ( message ) , context ) ; } } info ( message , context ? ) { if ( this . level >= LogLevel . Info ) { this . logMessage ( chalk . blue ( `info` ) , message , context ) ; } } verbose ( message , context ? ) { if ( this . level >= LogLevel . Verbose ) { this . logMessage ( chalk . magenta ( `verbose` ) , message , context ) ; } } debug ( message , context ? ) { if ( this . level >= LogLevel . Debug ) { this . logMessage ( chalk . magenta ( `debug` ) , message , context ) ; } } private logMessage ( prefix , message , context ? ) { process . stdout . write ( [ prefix , this . logTimestamp ( ) , this . logContext ( context ) , message , '\n' , ] . join ( ' ' ) ) ; } private logContext ( context ? ) { return chalk . cyan ( `[${ context || DEFAULT_CONTEXT }]` ) ; } private logTimestamp ( ) { if ( this . timestamp ) { const timestamp = new Date ( Date . now ( ) ) . toLocaleString ( undefined , this . localeStringOptions ) ; return chalk . gray ( timestamp + ' -' ) ; } else { return '' ; } } }