From 7813004b42d06f99aee5cdc34ca0b709e189c410 Mon Sep 17 00:00:00 2001 From: maitamgk Date: Thu, 5 Mar 2026 11:12:53 +0700 Subject: [PATCH] feat(utils): add application logger utility Co-authored-by: maitamgk <169973104+maitamgk@users.noreply.github.com> --- src/utils/logger.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/utils/logger.ts diff --git a/src/utils/logger.ts b/src/utils/logger.ts new file mode 100644 index 0000000..94b757b --- /dev/null +++ b/src/utils/logger.ts @@ -0,0 +1,28 @@ +// Application logger utility +type LogLevel = 'debug' | 'info' | 'warn' | 'error'; + +const LOG_COLORS: Record = { + debug: 'color: #9CA3AF', + info: 'color: #3B82F6', + warn: 'color: #F59E0B', + error: 'color: #EF4444', +}; + +class Logger { + private isDev = import.meta.env?.DEV ?? true; + + private log(level: LogLevel, message: string, data?: unknown): void { + if (!this.isDev && level === 'debug') return; + const timestamp = new Date().toISOString().slice(11, 23); + const prefix = '[' + timestamp + '] [' + level.toUpperCase() + ']'; + if (data) { console.log('%c' + prefix + ' ' + message, LOG_COLORS[level], data); } + else { console.log('%c' + prefix + ' ' + message, LOG_COLORS[level]); } + } + + debug(msg: string, data?: unknown) { this.log('debug', msg, data); } + info(msg: string, data?: unknown) { this.log('info', msg, data); } + warn(msg: string, data?: unknown) { this.log('warn', msg, data); } + error(msg: string, data?: unknown) { this.log('error', msg, data); } +} + +export const logger = new Logger();