/** * T034 演示:结构化日志和审计跟踪 * 演示结构化日志系统的完整功能 */ import { logger } from '../src/utils/logger.js' // 模拟 MonitoringEventManager class MockMonitoringEventManager { private events: any[] = [] addEvent(event: any): void { this.events.push(event) logger.info(`添加监控事件: ${event.type}`) } getAllEvents(): any[] { return this.events } } // 模拟文件系统操作 class MockFileSystem { private files = new Map() existsSync(path: string): boolean { return this.files.has(path) } mkdirSync(path: string, options?: any): void { this.files.set(path, '') logger.info(`创建目录: ${path}`) } appendFileSync(path: string, data: string): void { const existing = this.files.get(path) || '' this.files.set(path, existing + data) } readFileSync(path: string): string { return this.files.get(path) || '' } getFileSize(path: string): number { const content = this.files.get(path) || '' return Buffer.byteLength(content, 'utf8') } getFiles(): string[] { return Array.from(this.files.keys()) } } // 简化的结构化日志服务实现 class SimpleStructuredLoggingService { private config: any private stats: any private monitoringManager: MockMonitoringEventManager private mockFs: MockFileSystem private logQueue: any[] = [] private isProcessing = false constructor(monitoringManager: MockMonitoringEventManager) { this.monitoringManager = monitoringManager this.mockFs = new MockFileSystem() this.config = { enableAuditLogging: true, enablePerformanceLogging: true, enableSecurityLogging: true, enableTradingLogging: true, logLevel: 'info', logDirectory: './logs', maxLogFileSize: 100 * 1024 * 1024, retentionDays: 30, enableCompression: false } this.stats = { totalLogs: 0, auditLogs: 0, performanceLogs: 0, securityLogs: 0, tradingLogs: 0, errorLogs: 0, lastLogTime: 0, logFileSize: 0 } this.initializeLogging() } private initializeLogging(): void { logger.info('初始化结构化日志系统') try { // 创建日志目录 if (!this.mockFs.existsSync(this.config.logDirectory)) { this.mockFs.mkdirSync(this.config.logDirectory, { recursive: true }) } // 启动日志处理队列 this.startLogProcessing() // 记录系统启动日志 this.logAuditEvent({ eventType: 'system_startup', service: 'StructuredLoggingService', action: 'initialize', details: { config: this.config }, result: 'success' }) logger.info('结构化日志系统初始化完成') } catch (error: any) { logger.error(`结构化日志系统初始化失败: ${error.message}`) throw error } } private startLogProcessing(): void { setInterval(() => { this.processLogQueue() }, 1000) } private async processLogQueue(): Promise { if (this.isProcessing || this.logQueue.length === 0) { return } this.isProcessing = true try { const logsToProcess = [...this.logQueue] this.logQueue = [] for (const logEntry of logsToProcess) { await this.writeLogEntry(logEntry) } this.stats.totalLogs += logsToProcess.length this.stats.lastLogTime = Date.now() } catch (error: any) { logger.error(`处理日志队列失败: ${error.message}`) } finally { this.isProcessing = false } } private async writeLogEntry(logEntry: any): Promise { try { // 写入审计日志文件 if (this.config.enableAuditLogging) { await this.writeAuditLog(logEntry) } // 写入结构化日志文件 await this.writeStructuredLog(logEntry) // 更新统计信息 this.updateLogStats(logEntry) // 发送到监控系统 await this.sendToMonitoring(logEntry) } catch (error: any) { logger.error(`写入日志条目失败: ${error.message}`) } } private async writeAuditLog(logEntry: any): Promise { const auditLogPath = `${this.config.logDirectory}/audit.log` const logLine = JSON.stringify(logEntry) + '\n' this.mockFs.appendFileSync(auditLogPath, logLine) } private async writeStructuredLog(logEntry: any): Promise { const logPath = `${this.config.logDirectory}/structured.log` const logLine = JSON.stringify(logEntry) + '\n' this.mockFs.appendFileSync(logPath, logLine) } private updateLogStats(logEntry: any): void { switch (logEntry.eventType) { case 'audit': this.stats.auditLogs++ break case 'performance': this.stats.performanceLogs++ break case 'security': this.stats.securityLogs++ break case 'trading': this.stats.tradingLogs++ break } if (logEntry.result === 'failure') { this.stats.errorLogs++ } } private async sendToMonitoring(logEntry: any): Promise { try { const monitoringEvent = { eventId: logEntry.eventId, type: logEntry.eventType, payload: { service: logEntry.service, action: logEntry.action, details: logEntry.details, result: logEntry.result, duration: logEntry.duration }, severity: this.mapResultToSeverity(logEntry.result), createdAt: new Date(logEntry.timestamp) } this.monitoringManager.addEvent(monitoringEvent) } catch (error: any) { logger.error(`发送到监控系统失败: ${error.message}`) } } private mapResultToSeverity(result: string): 'INFO' | 'WARN' | 'ERROR' { switch (result) { case 'success': return 'INFO' case 'warning': return 'WARN' case 'failure': return 'ERROR' default: return 'INFO' } } logAuditEvent(event: any): void { const logEntry = { timestamp: new Date().toISOString(), eventId: `audit-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`, ...event } this.logQueue.push(logEntry) logger.info(`[AUDIT] ${event.service}: ${event.action} - ${event.result}`, { eventId: logEntry.eventId, eventType: event.eventType, details: event.details }) } logPerformanceEvent(event: any): void { const logEntry = { timestamp: new Date().toISOString(), eventId: `perf-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`, eventType: 'performance', ...event } this.logQueue.push(logEntry) logger.debug(`[PERF] ${event.service}: ${event.action} - ${event.duration}ms`, { eventId: logEntry.eventId, details: event.details }) } logSecurityEvent(event: any): void { const logEntry = { timestamp: new Date().toISOString(), eventId: `sec-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`, eventType: 'security', ...event } this.logQueue.push(logEntry) logger.warn(`[SEC] ${event.service}: ${event.action} - ${event.result}`, { eventId: logEntry.eventId, details: event.details }) } logTradingEvent(event: any): void { const logEntry = { timestamp: new Date().toISOString(), eventId: `trade-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`, eventType: 'trading', ...event } this.logQueue.push(logEntry) logger.info(`[TRADE] ${event.service}: ${event.action} - ${event.result}`, { eventId: logEntry.eventId, symbol: event.symbol, accountId: event.accountId, details: event.details }) } logSystemEvent(event: any): void { const logEntry = { timestamp: new Date().toISOString(), eventId: `sys-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`, eventType: 'system', ...event } this.logQueue.push(logEntry) logger.info(`[SYS] ${event.service}: ${event.action} - ${event.result}`, { eventId: logEntry.eventId, details: event.details }) } getLogStats(): any { return { ...this.stats } } getConfig(): any { return { ...this.config } } async cleanupOldLogs(): Promise { try { this.logAuditEvent({ eventType: 'maintenance', service: 'StructuredLoggingService', action: 'cleanupOldLogs', details: { retentionDays: this.config.retentionDays }, result: 'success' }) logger.info('旧日志文件清理完成') } catch (error: any) { this.logAuditEvent({ eventType: 'maintenance', service: 'StructuredLoggingService', action: 'cleanupOldLogs', details: { error: error.message }, result: 'failure' }) logger.error(`清理旧日志文件失败: ${error.message}`) } } getLogFiles(): string[] { return this.mockFs.getFiles() } getLogContent(fileName: string): string { return this.mockFs.readFileSync(fileName) } } async function main() { logger.info('=== T034 结构化日志和审计跟踪演示 ===') try { // 1. 创建监控事件管理器 const monitoringManager = new MockMonitoringEventManager() // 2. 创建结构化日志服务 const loggingService = new SimpleStructuredLoggingService(monitoringManager) // 3. 等待日志系统初始化 await new Promise(resolve => setTimeout(resolve, 1000)) // 4. 测试各种类型的日志记录 logger.info('测试审计日志记录...') loggingService.logAuditEvent({ eventType: 'account_sync', service: 'AccountManager', action: 'syncAccounts', accountId: 'pacifica-1', details: { accountCount: 2, syncDuration: 150 }, result: 'success' }) logger.info('测试性能日志记录...') loggingService.logPerformanceEvent({ service: 'HedgingCoordinator', action: 'executeHedge', duration: 250, details: { hedgeQuantity: 0.01, executionPrice: 108000 }, result: 'success' }) logger.info('测试安全日志记录...') loggingService.logSecurityEvent({ service: 'AccountManager', action: 'validateCredentials', details: { accountId: 'pacifica-1', validationResult: 'valid' }, result: 'success' }) logger.info('测试交易日志记录...') loggingService.logTradingEvent({ service: 'TradingEngine', action: 'executeOrder', accountId: 'pacifica-1', symbol: 'BTC-USD', details: { orderType: 'limit', quantity: 0.01, price: 108000 }, result: 'success', duration: 100 }) logger.info('测试系统日志记录...') loggingService.logSystemEvent({ service: 'SystemOrchestrator', action: 'startService', details: { serviceName: 'TradingEngineV2', startTime: Date.now() }, result: 'success' }) // 5. 等待日志处理 await new Promise(resolve => setTimeout(resolve, 2000)) // 6. 检查日志统计 const logStats = loggingService.getLogStats() logger.info('日志统计信息:', logStats) // 7. 检查日志文件 const logFiles = loggingService.getLogFiles() logger.info(`生成的日志文件: ${logFiles.length}`) logFiles.forEach(fileName => { logger.info(`- ${fileName}`) }) // 8. 检查监控事件 const monitoringEvents = monitoringManager.getAllEvents() logger.info(`监控事件数量: ${monitoringEvents.length}`) // 9. 测试日志清理功能 logger.info('测试日志清理功能...') await loggingService.cleanupOldLogs() // 10. 验证日志内容 logger.info('验证日志内容...') const auditLogContent = loggingService.getLogContent('./logs/audit.log') const structuredLogContent = loggingService.getLogContent('./logs/structured.log') const auditLogLines = auditLogContent.split('\n').filter(line => line.trim()).length const structuredLogLines = structuredLogContent.split('\n').filter(line => line.trim()).length logger.info(`验证结果:`) logger.info(`- 审计日志行数: ${auditLogLines}`) logger.info(`- 结构化日志行数: ${structuredLogLines}`) logger.info(`- 监控事件数量: ${monitoringEvents.length}`) logger.info(`- 总日志数量: ${logStats.totalLogs}`) logger.info(`- 审计日志数量: ${logStats.auditLogs}`) logger.info(`- 性能日志数量: ${logStats.performanceLogs}`) logger.info(`- 安全日志数量: ${logStats.securityLogs}`) logger.info(`- 交易日志数量: ${logStats.tradingLogs}`) logger.info(`- 错误日志数量: ${logStats.errorLogs}`) logger.info(`- 数据一致性: ${auditLogLines > 0 && structuredLogLines > 0 && monitoringEvents.length > 0 ? '✅' : '❌'}`) logger.info('=== T034 结构化日志和审计跟踪演示完成 ===') } catch (error: any) { logger.error('T034 演示失败:', { error: error.message, stack: error.stack }) process.exit(1) } } // 运行演示 if (import.meta.url === `file://${process.argv[1]}`) { main().catch(error => { logger.error('演示运行失败:', error) process.exit(1) }) }