import { window } from "vscode" ; export type LogLevel = "INFO" | "ERROR" ; export class Logger { private outputChannel = window . createOutputChannel ( "PHP Sniffer & Beautifier" ) ; private logLevel : LogLevel = "ERROR" ; private startTime : { [ key ] : Date ; } = { logger : new Date ( ) } ; public setOutputLevel ( logLevel ) { this . logLevel = logLevel ; } param message public logInfo ( message , data ? ) { if ( this . logLevel === "ERROR" ) { return ; } this . logMessage ( message , "INFO" ) ; if ( data ) { this . logObject ( data ) ; } } public logError ( message , error ? ) { this . logMessage ( message , "ERROR" ) ; if ( typeof error === "string" ) { this . outputChannel . appendLine ( error ) ; } else if ( error ?. message || error ?. stack ) { if ( error ?. message ) { this . logMessage ( error . message , "ERROR" ) ; } if ( error ?. stack ) { this . outputChannel . appendLine ( error . stack ) ; } } else if ( error ) { this . logObject ( error ) ; } } /**
     * Start a timer
     */ public time ( key ) { this . startTime [ key ] = new Date ( ) ; this . logInfo ( key + " running" ) ; } /**
     * Log timer result
     */ public timeEnd ( key ) { let endTime = new Date ( ) ; let timeDiff = endTime . valueOf ( ) - this . startTime [ key ] . valueOf ( ) ; let seconds = timeDiff / 1000 ; this . logInfo ( key + " ran for " + seconds + " seconds" ) ; } public show ( ) { this . outputChannel . show ( ) ; } private logObject ( data ) { const message = JSON . stringify ( data , null , 2 ) . trim ( ) ; this . outputChannel . appendLine ( message ) ; } param message private logMessage ( message , logLevel ) { const title = new Date ( ) . toLocaleTimeString ( ) ; this . outputChannel . appendLine ( `["${ logLevel }" - ${ title }] ${ message }` ) ; } }