import { OutputChannel } from "vscode" ; import { TestResult } from "../../model/enums/test-status.enum" ; import { LogLevel } from "../../model/enums/log-level.enum" ; export const OUTPUT_CHANNEL = "Test Explorer Logs" ; interface Ilog { message : string ; date : Date ; level : LogLevel ; } export class Logger { private outputChannel ? : OutputChannel ; constructor ( outputChannel , private readonly isDebuggingMode = false ) { this . outputChannel = outputChannel ; } public debug ( msg , ... params ) { if ( this . isDebuggingMode ) { global . console . log ( this . formatMsg ( msg , LogLevel . DEBUG ) ) ; if ( this . outputChannel !== undefined ) { this . outputChannel . appendLine ( this . formatMsg ( msg , LogLevel . DEBUG ) ) ; } } } public info ( msg , ... params ) { global . console . log ( this . formatMsg ( msg , LogLevel . INFO ) ) ; if ( this . outputChannel !== undefined ) { this . outputChannel . appendLine ( this . formatMsg ( msg , LogLevel . INFO ) ) ; } if ( params . length > 0 ) { if ( params [ 0 ] ! . addDividerForKarmaLogs ) { this . divideKarmaLogsContent ( ) ; } } } public warn ( msg , ... params ) { global . console . log ( this . formatMsg ( msg , LogLevel . WARN ) ) ; if ( this . outputChannel !== undefined ) { this . outputChannel . appendLine ( this . formatMsg ( msg , LogLevel . WARN ) ) ; } } public error ( msg , ... params ) { global . console . log ( this . formatMsg ( msg , LogLevel . ERROR ) ) ; if ( this . outputChannel !== undefined ) { this . outputChannel . appendLine ( this . formatMsg ( msg , LogLevel . ERROR ) ) ; } } public karmaLogs ( msg ) { if ( this . isDebuggingMode ) { global . console . log ( msg ) ; if ( this . outputChannel !== undefined ) { this . outputChannel . appendLine ( msg ) ; } } } public status ( status ) { let msg ; if ( status === TestResult . Success ) { msg = `[SUCCESS] ✅ Passed` ; } else if ( status === TestResult . Failed ) { msg = `[FAILURE] ❌ failed` ; } else { msg = `[SKIPPED] Test Skipped` ; } if ( this . outputChannel !== undefined ) { this . outputChannel . appendLine ( msg ) ; } } private formatMsg ( msg , loglevel ) { const { message , date , level } = { message : msg , date : new Date ( ) , level : loglevel , } ; return `[${ date . toLocaleTimeString ( ) }] ${ level . toUpperCase ( ) }: ${ message }` ; } private divideKarmaLogsContent ( ) { if ( this . outputChannel !== undefined ) { this . outputChannel . appendLine ( "******************************* Karma Logs *******************************" ) ; } } }