Skip to content

Commit

Permalink
Upgraded logger
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed Sep 4, 2024
1 parent 80039b4 commit 2dec5c6
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions src/logger.mts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ 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.
*/
interface Stringable {
toString(): string;
}

export enum LoggerSource {
gitHubRestApi = "githubREST",
}

/**
* Logger class to log messages to the console with different log levels and identifiable source.
*/
Expand All @@ -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
);
}
Expand Down

0 comments on commit 2dec5c6

Please sign in to comment.