Skip to content

Commit

Permalink
fix: type error in logger module
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-balfe committed Oct 17, 2024
1 parent 926d863 commit 5781cf8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"build": "bun run build.ts"
},
"devDependencies": {
"@types/bun": "latest"
"@types/bun": "latest",
"@types/chalk": "^2.2.0"
},
"peerDependencies": {
"typescript": "^5.0.0"
Expand Down
42 changes: 27 additions & 15 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import chalk from "chalk";

export enum LogLevel {
DEBUG,
INFO,
WARN,
ERROR,
}
export const LogLevel = {
DEBUG: "DEBUG" as const,
INFO: "INFO" as const,
WARN: "WARN" as const,
ERROR: "ERROR" as const,
};

export type LogLevelType = (typeof LogLevel)[keyof typeof LogLevel];

export class Logger {
private static instance: Logger;
private currentLevel: LogLevel = LogLevel.INFO;
private currentLevel: LogLevelType = LogLevel.INFO; // Default level

private constructor() {}

Expand All @@ -20,16 +22,26 @@ export class Logger {
return Logger.instance;
}

getLevel(): LogLevel {
getLevel(): LogLevelType {
return this.currentLevel;
}

setLevel(level: LogLevel) {
setLevel(level: LogLevelType) {
this.currentLevel = level;
}

private log(level: LogLevel, color: chalk.ChalkFunction, tag: string, ...messages: any[]) {
if (this.currentLevel <= level) {
private logLevelValue(level: LogLevelType): number {
const levels = {
[LogLevel.DEBUG]: 0,
[LogLevel.INFO]: 1,
[LogLevel.WARN]: 2,
[LogLevel.ERROR]: 3,
};
return levels[level];
}

private log(level: LogLevelType, color: (str: string) => string, tag: string, ...messages: any[]) {
if (this.logLevelValue(this.currentLevel) <= this.logLevelValue(level)) {
const formattedMessages = messages
.map((msg) => (typeof msg === "object" ? JSON.stringify(msg) : msg))
.join(" ");
Expand All @@ -38,19 +50,19 @@ export class Logger {
}

debug(...messages: any[]) {
this.log(LogLevel.DEBUG, chalk.gray, "DEBUG", ...messages);
this.log(LogLevel.DEBUG, chalk.gray, LogLevel.DEBUG, ...messages);
}

info(...messages: any[]) {
this.log(LogLevel.INFO, chalk.blue, "INFO", ...messages);
this.log(LogLevel.INFO, chalk.blue, LogLevel.INFO, ...messages);
}

warn(...messages: any[]) {
this.log(LogLevel.WARN, chalk.yellow, "WARN", ...messages);
this.log(LogLevel.WARN, chalk.yellow, LogLevel.WARN, ...messages);
}

error(...messages: any[]) {
this.log(LogLevel.ERROR, chalk.red, "ERROR", ...messages);
this.log(LogLevel.ERROR, chalk.red, LogLevel.ERROR, ...messages);
}

command(...messages: any[]) {
Expand Down

0 comments on commit 5781cf8

Please sign in to comment.