本文引用的文件
本文件面向AI有声书生成平台的错误处理与日志系统,系统性梳理全局错误处理机制、日志架构与配置、Sentry错误监控集成、请求日志与性能监控、审计日志实现以及最佳实践与故障排查。目标是帮助开发者快速理解并高效维护生产环境下的可观测性与稳定性。
围绕错误处理与日志的关键模块分布如下:
审计与分析:请求日志持久化、错误模式识别、自动修复建议、统计数据与清理策略。
graph TB
A["应用入口<br/>server/src/app.ts"] --> B["全局错误处理<br/>errorHandler.ts/.js"]
A --> C["Sentry错误处理中间件<br/>sentry.service.ts"]
A --> D["性能监控中间件<br/>performance.ts"]
A --> E["Winston HTTP日志中间件<br/>logger.service.ts"]
A --> F["请求日志中间件<br/>requestLogger.ts"]
F --> G["请求日志服务<br/>log.service.ts"]
G --> H["请求日志控制器<br/>log.controller.ts"]
B --> I["统一错误响应格式"]
C --> J["Sentry错误上报"]
D --> K["性能指标统计"]
E --> L["文件/控制台日志输出"]
图表来源
章节来源
章节来源
下图展示错误与日志在请求生命周期中的流转与落盘:
sequenceDiagram
participant Client as "客户端"
participant App as "Koa应用<br/>app.ts"
participant ReqLog as "请求日志中间件<br/>requestLogger.ts"
participant Perf as "性能监控<br/>performance.ts"
participant HttpLog as "HTTP日志中间件<br/>logger.service.ts"
participant ErrMW as "全局错误处理<br/>errorHandler.ts"
participant SentryMW as "Sentry错误处理<br/>sentry.service.ts"
participant LogSvc as "日志服务<br/>log.service.ts"
Client->>App : 发起HTTP请求
App->>ReqLog : 进入请求日志中间件
ReqLog->>Perf : 进入性能监控
Perf->>HttpLog : 进入HTTP日志
HttpLog->>SentryMW : 进入Sentry错误处理
SentryMW->>ErrMW : 进入全局错误处理
ErrMW-->>Client : 返回标准化错误响应
ReqLog->>LogSvc : 记录请求日志(含错误)
HttpLog-->>Client : 写入HTTP日志文件
图表来源
响应格式化策略
未捕获异常默认500,业务错误按错误类型映射对应状态码与业务码。
flowchart TD
Start(["进入全局错误处理"]) --> TryNext["调用下游中间件/路由"]
TryNext --> CatchErr{"是否抛出异常?"}
CatchErr --> |否| End(["结束"])
CatchErr --> |是| Normalize["标准化错误对象"]
Normalize --> SetStatus["设置状态码"]
SetStatus --> BuildResp["构建响应体<br/>code/message/data(+stack)"]
BuildResp --> End
图表来源
章节来源
目录与级别
日志目录自动创建;生产环境控制台级别提升至info,可通过LOG_LEVEL调整。
flowchart TD
Init["初始化Winston"] --> Console["控制台传输<br/>Console(level='info'|'debug')"]
Init --> ErrorFile["错误文件传输<br/>error.log(10MB*10)"]
Init --> Combined["常规文件传输<br/>combined.log(10MB*10)"]
Init --> HttpFile["HTTP文件传输<br/>http.log(10MB*5)"]
Console --> Logger["创建logger实例"]
ErrorFile --> Logger
Combined --> Logger
HttpFile --> Logger
图表来源
章节来源
Koa中间件集成
在Sentry错误处理中间件中注入请求方法、URL、用户信息,统一上报。
sequenceDiagram
participant App as "应用启动<br/>app.ts"
participant Sentry as "Sentry初始化<br/>sentry.service.ts"
participant MW as "Sentry错误处理中间件<br/>sentry.service.ts"
participant Route as "业务路由"
App->>Sentry : initSentry()
Sentry-->>App : 初始化完成
App->>MW : 注册中间件
MW->>Route : 处理请求
Route-->>MW : 抛出异常
MW->>Sentry : captureException(带上下文)
Sentry-->>MW : 上报完成
MW-->>Route : 重新抛出异常
图表来源
章节来源
审计能力
提供日志列表、最近错误、错误分析、统计数据、清理接口;支持按路径、状态、时间范围筛选。
classDiagram
class LogService {
+logs : RequestLog[]
+logRequest(log)
+getLogs(filter)
+getErrorLogs()
+getRecentErrors(count)
+analyzeErrors()
+autoAnalyzeError(log)
+clearLogs(olderThanHours?)
+getStats()
}
class RequestLog {
+requestId : string
+timestamp : Date
+method : string
+path : string
+query : string
+status : number
+responseTime : number
+responseSize : number
+level : LogLevel
+error? : string
+stack? : string
+headers : Record
+ip : string
+userAgent : string
+body? : any
}
LogService --> RequestLog : "管理"
图表来源
章节来源
指标路由
提供/getMetrics接口,返回当前指标与错误率百分比。
flowchart TD
PMStart["进入性能监控"] --> Measure["记录开始时间"]
Measure --> Next["调用下游"]
Next --> Done{"是否异常?"}
Done --> |否| Update["更新全局与端点指标"]
Update --> Slow{"是否慢请求?"}
Slow --> |是| Warn["控制台告警"]
Slow --> |否| Resp["设置X-Response-Time"]
Done --> |是| ErrUpdate["错误计数+1"]
ErrUpdate --> Throw["重新抛出错误"]
Resp --> PMEnd["结束"]
Warn --> PMEnd
Throw --> PMEnd
图表来源
章节来源
传播路径
业务层抛出自定义错误 → 全局错误处理中间件标准化响应 → Sentry中间件上报(如启用) → HTTP日志记录 → 请求日志持久化。
sequenceDiagram
participant Biz as "业务层"
participant ErrMW as "全局错误处理"
participant SentryMW as "Sentry中间件"
participant HttpLog as "HTTP日志"
participant ReqLog as "请求日志"
Biz->>Biz : 抛出AppError/子类
Biz->>ErrMW : 未捕获异常
ErrMW-->>Biz : 标准化响应(code,message,data)
ErrMW->>SentryMW : 上报异常(可选)
ErrMW->>HttpLog : 记录HTTP日志
ErrMW->>ReqLog : 记录请求日志
图表来源
章节来源
外部依赖
Winston用于结构化日志;Sentry用于错误监控与聚合;Koa中间件生态。
graph LR
App["app.ts"] --> EH["errorHandler.ts"]
App --> SMW["sentry.service.ts"]
App --> PM["performance.ts"]
App --> HL["logger.service.ts"]
App --> RL["requestLogger.ts"]
RL --> LS["log.service.ts"]
LS --> LC["log.controller.ts"]
图表来源
章节来源
章节来源
章节来源
该系统以Koa中间件为核心,结合Winston、Sentry与自研日志服务,实现了从异常捕获、标准化响应、结构化日志、性能监控到自动修复建议的全链路可观测体系。通过明确的错误分类、严格的日志轮转与采样策略、以及可扩展的错误模式识别,平台能够在复杂场景下保持高稳定性与可维护性。
章节来源