Skip to content

Instantly share code, notes, and snippets.

@Steellgold
Last active March 24, 2024 21:15
Show Gist options
  • Save Steellgold/1fe250ce2067c4d566aaaeb378e9a6bb to your computer and use it in GitHub Desktop.
Save Steellgold/1fe250ce2067c4d566aaaeb378e9a6bb to your computer and use it in GitHub Desktop.
const colors = {
reset: "\x1b[0m",
bold: "\x1b[1m",
thin: "\x1b[2m",
underscore: "\x1b[4m",
blink: "\x1b[5m",
reverse: "\x1b[7m",
hidden: "\x1b[8m",
fg: {
gray: "\x1b[30m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
magenta: "\x1b[35m",
cyan: "\x1b[36m",
white: "\x1b[37m"
},
bg: {
gray: "\x1b[40m",
red: "\x1b[41m",
green: "\x1b[42m",
yellow: "\x1b[43m",
blue: "\x1b[44m",
magenta: "\x1b[45m",
cyan: "\x1b[46m",
white: "\x1b[47m"
}
};
const spaces = (type: "GET" | "POST" | "DELETE" | "PUT" | "PATCH" | "HEAD"): string => {
const space = " ".repeat(6 - type.length);
return `${type}${space}`;
};
const colorByType = (type: "GET" | "POST" | "DELETE" | "PUT" | "PATCH" | "HEAD"): string => {
switch (type) {
case "GET":
return colors.fg.green;
case "POST":
return colors.fg.blue;
case "DELETE":
return colors.fg.red;
case "PUT":
return colors.fg.yellow;
case "PATCH":
return colors.fg.magenta;
case "HEAD":
return colors.fg.cyan;
default:
return colors.fg.gray;
}
};
export const Logger = {
info: (message: string) => {
console.log(` ${colors.fg.yellow}- ${colors.bold}${colors.fg.white}Info${colors.reset} ${colors.fg.gray}» ${colors.reset}${message}`);
},
success: (message: string) => {
console.log(` ${colors.fg.green}√ ${colors.bold}${colors.fg.white}Success${colors.reset} ${colors.fg.gray}» ${colors.reset}${message}`);
},
error: (message: string) => {
console.log(` ${colors.fg.red}x ${colors.bold}${colors.fg.white}Error ${colors.reset} ${colors.fg.gray}» ${colors.reset}${message}`);
},
route: (method: "GET" | "POST" | "DELETE" | "PUT" | "PATCH" | "HEAD", path: string) => {
console.log(`${colorByType(method)} √ ${spaces(method)} ${colors.reset} ${colors.fg.gray}» ${colors.reset}${path}`);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment