|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class ErrorLogger { |
|
|
constructor(componentName) { |
|
|
this.componentName = componentName; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log(message, details = {}, error = null) { |
|
|
const logEntry = { |
|
|
timestamp: new Date().toISOString(), |
|
|
component: this.componentName, |
|
|
level: 'error', |
|
|
message: message, |
|
|
details: details, |
|
|
stack: error?.stack || new Error().stack |
|
|
}; |
|
|
|
|
|
|
|
|
console.error(`[${logEntry.timestamp}] [${logEntry.component}] ERROR:`, message); |
|
|
|
|
|
if (Object.keys(details).length > 0) { |
|
|
console.error('Details:', details); |
|
|
} |
|
|
|
|
|
if (error) { |
|
|
console.error('Original error:', error.message); |
|
|
console.error('Stack trace:', error.stack); |
|
|
} |
|
|
|
|
|
return logEntry; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logValidationError(field, expected, actual) { |
|
|
return this.log('Validation Error', { |
|
|
field: field, |
|
|
expected: expected, |
|
|
actual: actual, |
|
|
type: 'validation' |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
warn(message, details = {}) { |
|
|
const logEntry = { |
|
|
timestamp: new Date().toISOString(), |
|
|
component: this.componentName, |
|
|
level: 'warning', |
|
|
message: message, |
|
|
details: details |
|
|
}; |
|
|
|
|
|
console.warn(`[${logEntry.timestamp}] [${logEntry.component}] WARNING:`, message); |
|
|
|
|
|
if (Object.keys(details).length > 0) { |
|
|
console.warn('Details:', details); |
|
|
} |
|
|
|
|
|
return logEntry; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
info(message, details = {}) { |
|
|
const logEntry = { |
|
|
timestamp: new Date().toISOString(), |
|
|
component: this.componentName, |
|
|
level: 'info', |
|
|
message: message, |
|
|
details: details |
|
|
}; |
|
|
|
|
|
console.log(`[${logEntry.timestamp}] [${logEntry.component}] INFO:`, message); |
|
|
|
|
|
if (Object.keys(details).length > 0) { |
|
|
console.log('Details:', details); |
|
|
} |
|
|
|
|
|
return logEntry; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function createLogger(componentName) { |
|
|
return new ErrorLogger(componentName); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function formatValidationErrors(errors) { |
|
|
if (!errors || errors.length === 0) { |
|
|
return 'No validation errors'; |
|
|
} |
|
|
|
|
|
return errors.map((err, index) => { |
|
|
return `${index + 1}. ${err.field}: Expected ${err.expected}, got ${err.actual}`; |
|
|
}).join('\n'); |
|
|
} |
|
|
|