UNPKG

autosnippet

Version:

Extract code patterns into a knowledge base for AI coding assistants

130 lines (129 loc) 3.55 kB
/** * 错误追踪系统 * 捕获、记录和分析应用程序错误 */ interface ErrorTrackerConfig { logDirectory: string; maxErrorsInMemory: number; enableFileLogging: boolean; enableConsoleLogging: boolean; alertThreshold: number; } interface ErrorData { message: string; stack?: string; type: string; statusCode: number; route?: string; method?: string; path?: string; query?: Record<string, unknown>; ip?: string; userAgent?: string; timestamp: string; severity: string; } export declare class ErrorTracker { criticalErrors: ErrorData[]; recentErrors: ErrorData[]; config: ErrorTrackerConfig; errorCounts: Map<string, number>; errors: ErrorData[]; reportInterval: ReturnType<typeof setInterval>; constructor(options?: Partial<ErrorTrackerConfig & { logDirectory: string; maxErrorsInMemory: number; enableFileLogging: boolean; enableConsoleLogging: boolean; alertThreshold: number; }>); /** 确保日志目录存在 */ _ensureLogDirectory(): void; /** Express 错误处理中间件 */ errorHandler(): (err: { message: string; stack?: string; name?: string; statusCode?: number; code?: string; }, req: { method: string; path: string; query: Record<string, unknown>; ip: string; get: (header: string) => string | undefined; }, res: { status: (code: number) => { json: (body: unknown) => void; }; statusCode: number; }, _next: unknown) => void; /** 记录错误 */ trackError(errorData: ErrorData): void; /** 写入文件 */ _writeToFile(errorData: ErrorData): void; /** 检查告警阈值 */ _checkAlertThreshold(): void; /** 生成错误报告 */ _generateReport(): void; /** 获取最常见错误类型 */ _getTopErrorTypes(limit?: number): { type: string; count: number; }[]; /** 获取错误统计 */ getStats(): { summary: { totalErrors: number; criticalErrors: number; lastHourErrors: number; last24HoursErrors: number; uniqueErrorTypes: number; }; topErrorTypes: { type: string; count: number; }[]; recentErrors: { type: string; message: string; route: string | undefined; statusCode: number; severity: string; timestamp: string; }[]; criticalErrors: { type: string; message: string; route: string | undefined; timestamp: string; }[]; errorsByRoute: { route: any; count: any; }[]; }; /** 按路由统计错误 */ _getErrorsByRoute(): { route: any; count: any; }[]; /** 清除错误记录 */ clearErrors(): void; /** 搜索错误 */ searchErrors(options?: { type?: string; route?: string; severity?: string; startDate?: string; endDate?: string; limit?: number; }): ErrorData[]; /** 停止错误追踪 */ shutdown(): void; } /** 初始化错误追踪 */ export declare function initErrorTracker(options?: Partial<ErrorTrackerConfig>): ErrorTracker; /** 获取错误追踪实例 */ export declare function getErrorTracker(): ErrorTracker; export default ErrorTracker;