1 changed files with 41 additions and 0 deletions
@ -0,0 +1,41 @@ |
|||
type LogLevel = "debug" | "info" | "warn" | "error" | "none"; |
|||
|
|||
class Logger { |
|||
private level: LogLevel = "debug"; |
|||
|
|||
setLevel(level: LogLevel): void { |
|||
this.level = level; |
|||
} |
|||
|
|||
debug(...args: any[]): void { |
|||
if (this.shouldLog("debug")) { |
|||
console.debug(...args); |
|||
} |
|||
} |
|||
|
|||
info(...args: any[]): void { |
|||
if (this.shouldLog("info")) { |
|||
console.info(...args); |
|||
} |
|||
} |
|||
|
|||
warn(...args: any[]): void { |
|||
if (this.shouldLog("warn")) { |
|||
console.warn(...args); |
|||
} |
|||
} |
|||
|
|||
error(...args: any[]): void { |
|||
if (this.shouldLog("error")) { |
|||
console.error(...args); |
|||
} |
|||
} |
|||
|
|||
private shouldLog(level: LogLevel): boolean { |
|||
const levels: LogLevel[] = ["debug", "info", "warn", "error", "none"]; |
|||
return levels.indexOf(level) >= levels.indexOf(this.level); |
|||
} |
|||
} |
|||
|
|||
export const logger = new Logger(); |
|||
export default logger; |
|||
Loading…
Reference in new issue