Skip to content

Instantly share code, notes, and snippets.

@toky-nomena
Last active April 25, 2024 14:08
Show Gist options
  • Save toky-nomena/279b93f86b3c3b32cfd575ba04f7acdc to your computer and use it in GitHub Desktop.
Save toky-nomena/279b93f86b3c3b32cfd575ba04f7acdc to your computer and use it in GitHub Desktop.
const enum LogLevel {
Info = 'info',
Warning = 'warning',
Error = 'error',
}
class Logger {
private static logStyle = {
info: 'color: #3498db; font-weight: bold;',
warning: 'color: #f39c12; font-weight: bold;',
error: 'color: #e74c3c; font-weight: bold;',
};
static log(level: LogLevel, message: string, title?: string) {
const style = Logger.logStyle[level] || '';
const titleStyle = 'font-weight: bold;';
const logTitle = title ? `%c[${level}] %c${title}` : `%c[${level}]`;
console.log(logTitle, style, titleStyle, title, message);
}
static info(message: string, title?: string) {
Logger.log(LogLevel.Info, message, title);
}
static warning(message: string, title?: string) {
Logger.log(LogLevel.Warning, message, title);
}
static error(message: string, title?: string) {
Logger.log(LogLevel.Error, message, title);
}
}
// Example usage:
Logger.info('This is an info message', 'Info Title');
Logger.warning('This is a warning message', 'Warning Title');
Logger.error('This is an error message', 'Error Title');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment