-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new log option to set the log level
- Loading branch information
1 parent
e6697b8
commit 694c278
Showing
5 changed files
with
116 additions
and
31 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,56 @@ | ||
export type LogLevel = { | ||
level?: 'info' | 'error' | 'debug' | ||
} | ||
|
||
const LOG_LEVELS = { | ||
debug: 0, | ||
info: 1, | ||
error: 2, | ||
} | ||
|
||
export class Logger { | ||
private readonly level: number | ||
|
||
constructor(options: LogLevel = {}) { | ||
this.level = this.toLogLevel(options.level) | ||
} | ||
|
||
public info(namespace: string, name: string, data: Record<any, any>) { | ||
if (LOG_LEVELS.info >= this.level) { | ||
console.info(this.formatPrefix(namespace, name), this.serialize(data)) | ||
} | ||
} | ||
|
||
public error(namespace: string, name: string, data: Record<any, any>) { | ||
if (LOG_LEVELS.error >= this.level) { | ||
console.error(this.formatPrefix(namespace, name), this.serialize(data)) | ||
} | ||
} | ||
|
||
public debug(namespace: string, name: string, data: Record<any, any>) { | ||
if (LOG_LEVELS.debug >= this.level) { | ||
console.debug(this.formatPrefix(namespace, name), this.serialize(data)) | ||
} | ||
} | ||
|
||
private toLogLevel(level?: 'info' | 'error' | 'debug'): number { | ||
if (level === 'info') { | ||
return LOG_LEVELS.info | ||
} | ||
if (level === 'error') { | ||
return LOG_LEVELS.error | ||
} | ||
if (level === 'debug') { | ||
return LOG_LEVELS.debug | ||
} | ||
return 100 | ||
} | ||
|
||
private formatPrefix(namespace: string, name: string) { | ||
return `[${namespace}][${name}]` | ||
} | ||
|
||
private serialize(data: Record<any, any>) { | ||
return JSON.stringify(data) | ||
} | ||
} |
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