Skip to content

Instantly share code, notes, and snippets.

@airburst
Last active May 30, 2023 17:32
Show Gist options
  • Save airburst/1404d07ebdb9448477d69a08b32f9150 to your computer and use it in GitHub Desktop.
Save airburst/1404d07ebdb9448477d69a08b32f9150 to your computer and use it in GitHub Desktop.
type LogLevel = "log" | "info" | "error" | "debug" | "warning";
class Logger {
constructor() {
this.module = console;
}
format(message: string, level: LogLevel = "log"): string {
const dateTime = new Date().toISOString();
return JSON.stringify({ time: dateTime, level, message });
}
log(message: string, level: LogLevel = "log") {
this.module.log(this.format(message, level));
}
info(message: string) {
this.log(message, "info");
}
warning(message: string) {
this.log(message, "warning");
}
error(message: string) {
this.log(message, "error");
}
debug(message: string) {
this.log(message, "debug");
}
}
const logger = new Logger();
logger.log("test");
logger.info("Nothing important");
logger.warning("Warning!");
logger.error("Alert!");
logger.debug("Break point reached");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment