-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a logger and some debug statements (#35)
- Loading branch information
Showing
3 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
export enum LogLevel { | ||
DEBUG = 4, | ||
INFO = 3, | ||
WARN = 2, | ||
ERROR = 1, | ||
NONE = 0, | ||
} | ||
|
||
export class Logger { | ||
static logLevel: LogLevel = LogLevel.NONE; | ||
static logFilter?: string; | ||
|
||
static setLogLevel(logLevel: LogLevel) { | ||
this.logLevel = logLevel; | ||
} | ||
|
||
static setLogFilter(pattern: string) { | ||
this.logFilter = pattern; | ||
} | ||
|
||
static error(msg: string, ...args: any[]) { | ||
if (!this.shouldLog(msg, LogLevel.ERROR)) return; | ||
|
||
console.error(msg, ...args); | ||
} | ||
|
||
static warn(msg: string, ...args: any[]) { | ||
if (!this.shouldLog(msg, LogLevel.WARN)) return; | ||
|
||
console.warn(msg, ...args); | ||
} | ||
|
||
static debug(msg: string, ...args: any[]) { | ||
if (!this.shouldLog(msg, LogLevel.DEBUG)) return; | ||
|
||
console.debug(msg, ...args); | ||
} | ||
|
||
static info(msg: string, ...args: any[]) { | ||
if (!this.shouldLog(msg, LogLevel.INFO)) return; | ||
|
||
console.info(msg, ...args); | ||
} | ||
|
||
private static shouldLog(msg: string, level: LogLevel) { | ||
if (this.logLevel < level) return false; | ||
if (this.logFilter && !msg.includes(this.logFilter)) return false; | ||
|
||
return true; | ||
} | ||
} |