// src/services/logger.ts - Sistema de logging
import type { LogLevel } from '../types.ts';
const LOG_LEVEL = (process.env.LOG_LEVEL || 'info') as LogLevel;
const levels: Record<LogLevel, number> = {
debug: 0,
info: 1,
warn: 2,
error: 3
};
function shouldLog(level: LogLevel): boolean {
return levels[level] >= levels[LOG_LEVEL];
}
function formatDate(): string {
return new Date().toISOString();
}
function colorize(level: LogLevel, text: string): string {
const colors: Record<LogLevel, string> = {
debug: '\x1b[36m', // cyan
info: '\x1b[32m', // green
warn: '\x1b[33m', // yellow
error: '\x1b[31m' // red
};
return `${colors[level]}${text}\x1b[0m`;
}
export function log(level: LogLevel, message: string, data?: Record<string, any>): void {
if (!shouldLog(level)) return;
const timestamp = formatDate();
const prefix = colorize(level, `[${level.toUpperCase()}]`);
const dataStr = data ? ` ${JSON.stringify(data)}` : '';
console.error(`${timestamp} ${prefix} ${message}${dataStr}`);
}
export const logger = {
debug: (message: string, data?: Record<string, any>) => log('debug', message, data),
info: (message: string, data?: Record<string, any>) => log('info', message, data),
warn: (message: string, data?: Record<string, any>) => log('warn', message, data),
error: (message: string, data?: Record<string, any>) => log('error', message, data),
syncStart: (source: string) => {
console.error('\n' + '='.repeat(50));
log('info', `Iniciando sincronizacion: ${source}`);
console.error('='.repeat(50));
},
syncEnd: (result: { success: boolean; added: number; updated: number; deleted?: number; errors: string[] }) => {
console.error('-'.repeat(50));
if (result.success) {
log('info', `Sincronizacion completada`, {
agregados: result.added,
actualizados: result.updated,
eliminados: result.deleted || 0
});
} else {
log('error', `Sincronizacion fallida`, { errors: result.errors });
}
console.error('='.repeat(50) + '\n');
}
};