diff --git a/src/logger.mts b/src/logger.mts index 5a3d9275..cad6d654 100644 --- a/src/logger.mts +++ b/src/logger.mts @@ -12,6 +12,8 @@ const magenta = "\x1b[35m"; // ANSI escape code to reset color const reset = "\x1b[0m"; +const EXT_LOG_PREFIX = "[raspberry-pi-pico]"; + /** * Interface for objects that can be converted to a string with the toString() method. */ @@ -19,6 +21,10 @@ interface Stringable { toString(): string; } +export enum LoggerSource { + gitHubRestApi = "githubREST", +} + /** * Logger class to log messages to the console with different log levels and identifiable source. */ @@ -44,47 +50,82 @@ export default class Logger { message: string | undefined, ...optionalParams: Stringable[] ): void { - console.log(`[raspberry-pi-pico] ${message}`, ...optionalParams); + console.log(`${EXT_LOG_PREFIX} ${message}`, ...optionalParams); } - private shouldLog(level: LogLevel): boolean { + private static shouldLog(level: LogLevel): boolean { const levels: LogLevel[] = ["debug", "info", "warn", "error"]; return levels.indexOf(level) >= levels.indexOf(logLevel); } public info(message: string, ...optionalParams: Stringable[]): void { - if (this.shouldLog("info")) { - console.info(`[INFO] [${this.className}] ${message}`, ...optionalParams); + Logger.info(this.className, message, ...optionalParams); + } + + public static info( + source: string | LoggerSource, + message: string, + ...optionalParams: Stringable[] + ): void { + if (Logger.shouldLog("info")) { + console.info( + `[${blue}INFO${reset}] [${EXT_LOG_PREFIX} - ${source}] ${message}`, + ...optionalParams + ); } } public warn(message: string, ...optionalParams: Stringable[]): void { - if (this.shouldLog("warn")) { + Logger.warn(this.className, message, ...optionalParams); + } + + public static warn( + source: string | LoggerSource, + message: string, + ...optionalParams: Stringable[] + ): void { + if (Logger.shouldLog("warn")) { console.warn( - `[${yellow}WARN${reset}] [${this.className}] ${message}`, + `[${yellow}WARN${reset}] [${EXT_LOG_PREFIX} - ${source}] ${message}`, ...optionalParams ); } } public error(message: string | Error, ...optionalParams: Stringable[]): void { - if (this.shouldLog("error")) { + Logger.error(this.className, message, ...optionalParams); + } + + public static error( + source: string | LoggerSource, + message: string | Error, + ...optionalParams: Stringable[] + ): void { + if (Logger.shouldLog("error")) { if (message instanceof Error) { message = message.message; } console.error( - `[${red}ERROR${reset}] [${this.className}] ${message}`, + `[${red}ERROR${reset}] [${EXT_LOG_PREFIX} - ${source}] ${message}`, ...optionalParams ); } } public debug(message: string, ...optionalParams: Stringable[]): void { + Logger.debug(this.className, message, ...optionalParams); + } + + public static debug( + source: string | LoggerSource, + message: string, + ...optionalParams: Stringable[] + ): void { if (this.shouldLog("debug")) { console.debug( - `[DEBUG] [${this.className}] ${message}`, + `[${magenta}DEBUG${reset}] [${EXT_LOG_PREFIX} - ${source}] ${message}`, ...optionalParams ); }