import { EErrorEngineTypes } from "./enum/EErrorEngineTypes"; import { EMessageType } from "./enum/EMessageType"; import { ILogger} from "./interface/ILogger"; import { TCallbackMessage } from "./type/TCallbackMessage"; export class Logger implements ILogger { public debug : boolean = false; public engines: Array = [EErrorEngineTypes.CONSOLE, EErrorEngineTypes.VIEW]; public callbackMessage?: Function; constructor(engines : Array){ this.engines = engines; } public error(message : TCallbackMessage) : void{ message.type = EMessageType.ERROR; this.doLog(message); } public info(message: TCallbackMessage) : void { message.type = EMessageType.INFO; this.doLog(message); } public warning(message: TCallbackMessage) : void { message.type = EMessageType.WARNING; this.doLog(message); } public log(message: TCallbackMessage) : void { switch (message.type) { case EMessageType.ERROR: this.error(message); break; case EMessageType.INFO: this.info(message); break; case EMessageType.WARNING: this.warning(message); break; default: this.info(message); break; } } public doLog(message: TCallbackMessage){ this.engines.forEach(engine => { switch (engine) { case EErrorEngineTypes.CONSOLE: this.doConsoleLog(message); break; case EErrorEngineTypes.SENTRY: //TODO:integration with sentry break; case EErrorEngineTypes.VIEW: this.doNotifyView(message); break; default: break; } }); } public doConsoleLog(message: TCallbackMessage){ switch (message.type) { case EMessageType.ERROR: this.colorLog(message, "error"); break; case EMessageType.INFO: this.colorLog(message, "info"); break; case EMessageType.WARNING: this.colorLog(message, "warning"); break; default: break; } } public colorLog(message : TCallbackMessage, color : string) { color = color || "black"; switch (color) { case "success": color = "Green"; break; case "info": color = "DodgerBlue"; break; case "error": color = "Red"; break; case "warning": color = "Orange"; break; default: color = color; } console.log("%c" + "[AgoraModule] - "+message.body, "color:" + color, message); } public doNotifyView(message: TCallbackMessage) { if(this.callbackMessage){ this.callbackMessage(message); } } public getCallbackMessage(): any { return this.callbackMessage; } public setCallbackMessage(value: Function) { this.callbackMessage = value; } }