56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
// Simple in-memory logger for API requests
|
|
|
|
interface LogEntry {
|
|
timestamp: string;
|
|
method: string;
|
|
endpoint: string;
|
|
statusCode?: number;
|
|
responseTime?: number;
|
|
requestBody?: any;
|
|
error?: string;
|
|
apiKey?: string; // This will be redacted in the UI
|
|
}
|
|
|
|
class Logger {
|
|
private logs: LogEntry[] = [];
|
|
private maxLogs: number = 100;
|
|
|
|
log(entry: LogEntry): void {
|
|
// Remove sensitive information before storing
|
|
const sanitizedEntry = { ...entry };
|
|
|
|
// If apiKey exists, replace it with a redacted version
|
|
if (sanitizedEntry.apiKey) {
|
|
const key = sanitizedEntry.apiKey;
|
|
sanitizedEntry.apiKey = key.substring(0, 4) + '...' + key.substring(key.length - 4);
|
|
}
|
|
|
|
// Add to the beginning for reverse chronological order
|
|
this.logs.unshift(sanitizedEntry);
|
|
|
|
// Trim logs if they exceed the maximum
|
|
if (this.logs.length > this.maxLogs) {
|
|
this.logs = this.logs.slice(0, this.maxLogs);
|
|
}
|
|
}
|
|
|
|
getLogs(): LogEntry[] {
|
|
// Return a copy of the logs to prevent modification
|
|
return [...this.logs];
|
|
}
|
|
|
|
clearLogs(): void {
|
|
this.logs = [];
|
|
}
|
|
}
|
|
|
|
// Singleton instance
|
|
let loggerInstance: Logger | null = null;
|
|
|
|
export function getLogger(): Logger {
|
|
if (!loggerInstance) {
|
|
loggerInstance = new Logger();
|
|
}
|
|
return loggerInstance;
|
|
}
|