Skip to content

Commit 85345ee

Browse files
authored
Merge pull request #23 from maitamdev/pair/logger
feat(utils): add logger
2 parents f65a659 + 7813004 commit 85345ee

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/utils/logger.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Application logger utility
2+
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
3+
4+
const LOG_COLORS: Record<LogLevel, string> = {
5+
debug: 'color: #9CA3AF',
6+
info: 'color: #3B82F6',
7+
warn: 'color: #F59E0B',
8+
error: 'color: #EF4444',
9+
};
10+
11+
class Logger {
12+
private isDev = import.meta.env?.DEV ?? true;
13+
14+
private log(level: LogLevel, message: string, data?: unknown): void {
15+
if (!this.isDev && level === 'debug') return;
16+
const timestamp = new Date().toISOString().slice(11, 23);
17+
const prefix = '[' + timestamp + '] [' + level.toUpperCase() + ']';
18+
if (data) { console.log('%c' + prefix + ' ' + message, LOG_COLORS[level], data); }
19+
else { console.log('%c' + prefix + ' ' + message, LOG_COLORS[level]); }
20+
}
21+
22+
debug(msg: string, data?: unknown) { this.log('debug', msg, data); }
23+
info(msg: string, data?: unknown) { this.log('info', msg, data); }
24+
warn(msg: string, data?: unknown) { this.log('warn', msg, data); }
25+
error(msg: string, data?: unknown) { this.log('error', msg, data); }
26+
}
27+
28+
export const logger = new Logger();

0 commit comments

Comments
 (0)