logging
This commit is contained in:
@@ -1,39 +1,81 @@
|
||||
import { Logger } from '../../interfaces/Logger';
|
||||
|
||||
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
||||
|
||||
export class ConsoleLogger implements Logger {
|
||||
private formatMessage(level: string, message: string, context?: unknown): string {
|
||||
const timestamp = new Date().toISOString();
|
||||
const contextStr = context ? ` | ${JSON.stringify(context)}` : '';
|
||||
return `[${timestamp}] ${level.toUpperCase()}: ${message}${contextStr}`;
|
||||
private readonly COLORS: Record<LogLevel, string> = {
|
||||
debug: '#888888',
|
||||
info: '#00aaff',
|
||||
warn: '#ffaa00',
|
||||
error: '#ff4444',
|
||||
};
|
||||
|
||||
private readonly EMOJIS: Record<LogLevel, string> = {
|
||||
debug: '🐛',
|
||||
info: 'ℹ️',
|
||||
warn: '⚠️',
|
||||
error: '❌',
|
||||
};
|
||||
|
||||
private readonly PREFIXES: Record<LogLevel, string> = {
|
||||
debug: 'DEBUG',
|
||||
info: 'INFO',
|
||||
warn: 'WARN',
|
||||
error: 'ERROR',
|
||||
};
|
||||
|
||||
private shouldLog(level: LogLevel): boolean {
|
||||
if (process.env.NODE_ENV === 'test') return level === 'error';
|
||||
if (process.env.NODE_ENV === 'production') return level !== 'debug';
|
||||
return true;
|
||||
}
|
||||
|
||||
private formatOutput(level: LogLevel, source: string, message: string, context?: unknown, error?: Error): void {
|
||||
const color = this.COLORS[level];
|
||||
const emoji = this.EMOJIS[level];
|
||||
const prefix = this.PREFIXES[level];
|
||||
|
||||
console.groupCollapsed(`%c${emoji} [${source.toUpperCase()}] ${prefix}: ${message}`, `color: ${color}; font-weight: bold;`);
|
||||
|
||||
console.log(`%cTimestamp:`, 'color: #666; font-weight: bold;', new Date().toISOString());
|
||||
console.log(`%cSource:`, 'color: #666; font-weight: bold;', source);
|
||||
|
||||
if (context) {
|
||||
console.log(`%cContext:`, 'color: #666; font-weight: bold;');
|
||||
console.dir(context, { depth: 3, colors: true });
|
||||
}
|
||||
|
||||
if (error) {
|
||||
console.log(`%cError Details:`, 'color: #666; font-weight: bold;');
|
||||
console.log(`%cType:`, 'color: #ff4444; font-weight: bold;', error.name);
|
||||
console.log(`%cMessage:`, 'color: #ff4444; font-weight: bold;', error.message);
|
||||
|
||||
if (process.env.NODE_ENV === 'development' && error.stack) {
|
||||
console.log(`%cStack Trace:`, 'color: #666; font-weight: bold;');
|
||||
console.log(error.stack);
|
||||
}
|
||||
}
|
||||
|
||||
console.groupEnd();
|
||||
}
|
||||
|
||||
debug(message: string, context?: unknown): void {
|
||||
// Always log debug in development and test environments
|
||||
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
|
||||
console.debug(this.formatMessage('debug', message, context));
|
||||
}
|
||||
if (!this.shouldLog('debug')) return;
|
||||
this.formatOutput('debug', 'website', message, context);
|
||||
}
|
||||
|
||||
info(message: string, context?: unknown): void {
|
||||
// Always log info - we need transparency in all environments
|
||||
console.info(this.formatMessage('info', message, context));
|
||||
if (!this.shouldLog('info')) return;
|
||||
this.formatOutput('info', 'website', message, context);
|
||||
}
|
||||
|
||||
warn(message: string, context?: unknown): void {
|
||||
console.warn(this.formatMessage('warn', message, context));
|
||||
if (!this.shouldLog('warn')) return;
|
||||
this.formatOutput('warn', 'website', message, context);
|
||||
}
|
||||
|
||||
error(message: string, error?: Error, context?: unknown): void {
|
||||
const errorStr = error ? ` | Error: ${error.message}` : '';
|
||||
console.error(this.formatMessage('error', message, context) + errorStr);
|
||||
|
||||
// In development, also show enhanced error info
|
||||
if (process.env.NODE_ENV === 'development' && error) {
|
||||
console.groupCollapsed(`%c[ERROR DETAIL] ${message}`, 'color: #ff4444; font-weight: bold;');
|
||||
console.log('Error Object:', error);
|
||||
console.log('Stack Trace:', error.stack);
|
||||
console.log('Context:', context);
|
||||
console.groupEnd();
|
||||
}
|
||||
if (!this.shouldLog('error')) return;
|
||||
this.formatOutput('error', 'website', message, context, error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user