-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f0db4f
commit 828fbe3
Showing
5 changed files
with
173 additions
and
58 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,61 @@ | ||
const logLevels = ['debug', 'info', 'warn', 'error', 'none'] as const | ||
export type LogLevel = (typeof logLevels)[number] | ||
|
||
export class Logger { | ||
private level: LogLevel | ||
|
||
constructor(level: LogLevel = 'none') { | ||
this.level = level | ||
} | ||
|
||
setLevel(level: LogLevel) { | ||
this.level = level | ||
} | ||
|
||
coerceLevel(level: string): LogLevel { | ||
if (logLevels.includes(level.toLocaleUpperCase() as LogLevel)) { | ||
return level.toLocaleUpperCase() as LogLevel | ||
} | ||
return 'info' | ||
} | ||
|
||
log(level: string, message: string, ...args: any[]) { | ||
const logLevel = this.coerceLevel(level) | ||
if (!this.shouldLog(logLevel)) { | ||
return | ||
} | ||
const consoleFn = console[logLevel] | ||
consoleFn(`[${logLevel.toUpperCase()}] ${message}`, ...args) | ||
} | ||
|
||
debug(message: string, ...args: any[]) { | ||
this.log('debug', message, ...args) | ||
} | ||
|
||
info(message: string, ...args: any[]) { | ||
this.log('info', message, ...args) | ||
} | ||
|
||
warn(message: string, ...args: any[]) { | ||
this.log('warn', message, ...args) | ||
} | ||
|
||
error(message: string, ...args: any[]) { | ||
this.log('error', message, ...args) | ||
} | ||
|
||
private shouldLog( | ||
messageLevel: LogLevel, | ||
): messageLevel is Exclude<LogLevel, 'none'> { | ||
const levels: LogLevel[] = ['debug', 'info', 'warn', 'error', 'none'] | ||
const messageLevelIndex = levels.indexOf(messageLevel) | ||
const currentLevelIndex = levels.indexOf(this.level) | ||
return ( | ||
currentLevelIndex <= messageLevelIndex && | ||
this.level !== 'none' && | ||
messageLevel !== 'none' | ||
) | ||
} | ||
} | ||
|
||
export const logger = new Logger() |
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