Created
January 10, 2023 22:27
-
-
Save kingnebby/7617f5e2b1db84a7ea005341d5ec9955 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Logger.ts | |
export const LogLevel = { | |
INFO: 30, | |
DEBUG: 20, | |
}; | |
export class Logger { | |
// default log level | |
level = LogLevel.INFO; | |
info(data: object, msg: string) { | |
const logLine = this.serialize(data, msg); | |
this.log(LogLevel.INFO, logLine); | |
} | |
debug(data: object, msg: string) { | |
const logLine = this.serialize(data, msg); | |
this.log(LogLevel.DEBUG, logLine); | |
} | |
serialize(data: object, msg: string): string { | |
const logObject = { msg, meta: data }; | |
const serializedLog = JSON.stringify(logObject); | |
return serializedLog; | |
} | |
// log the line to stdout | |
log(level: number, json: string): void { | |
if (level >= this.level) { | |
console.log(json.slice(0, 100)); // don't log huge output | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment