Skip to content

Instantly share code, notes, and snippets.

@kauefraga
Last active November 17, 2023 21:54
Show Gist options
  • Save kauefraga/5ff6d7b0e83c62d3c0de37a9b5862c78 to your computer and use it in GitHub Desktop.
Save kauefraga/5ff6d7b0e83c62d3c0de37a9b5862c78 to your computer and use it in GitHub Desktop.
Simple console output wrapper without any dependency. (typescript)
type WhoIsLogging = 'SERVER' | 'MIDDLEWARE' | 'API' | 'DATABASE';
// ANSI escape code
const reset = '\x1b[0m';
// Foreground colors
const red = '\x1b[31m';
const green = '\x1b[32m';
const yellow = '\x1b[33m';
const info = (who: WhoIsLogging, message: string) => {
console.info(`[${green}${who}${reset}] ${message}`);
};
const warn = (who: WhoIsLogging, message: string) => {
console.warn(`[${yellow}${who}${reset}] ${message}`);
};
const error = (who: WhoIsLogging, message: string) => {
console.error(`[${red}${who}${reset}] ${message}`);
};
/* Examples
info('API', 'generating shortened url');
warn('SERVER', 'booting up...');
error('DATABASE', 'could not handle the amount of reading');
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment