create-dp-koa 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -0
- package/index.mjs +97 -0
- package/package.json +33 -0
- package/template/.env.development +9 -0
- package/template/.env.production +12 -0
- package/template/.github/workflows/ci-cd.yml +182 -0
- package/template/.trae/documents/controller_development_plan.md +386 -0
- package/template/.trae/skills/00-backend-core.skill.md +50 -0
- package/template/.trae/skills/01-backend-skill-router.skill.md +55 -0
- package/template/.trae/skills/10-backend-api.skill.md +54 -0
- package/template/.trae/skills/11-backend-controller-recipes.skill.md +107 -0
- package/template/.trae/skills/20-backend-repository.skill.md +25 -0
- package/template/.trae/skills/21-backend-service.skill.md +135 -0
- package/template/.trae/skills/25-backend-comments-and-doc.skill.md +97 -0
- package/template/.trae/skills/30-backend-validation.skill.md +320 -0
- package/template/.trae/skills/40-backend-error-logging.skill.md +21 -0
- package/template/.trae/skills/50-backend-bootstrap-lifecycle.skill.md +90 -0
- package/template/.trae/skills/60-backend-router-registration.skill.md +71 -0
- package/template/.trae/skills/70-backend-middleware.skill.md +98 -0
- package/template/.trae/skills/80-backend-utils-and-libs.skill.md +90 -0
- package/template/.trae/skills/85-backend-plugins.rule.md +64 -0
- package/template/.trae/skills/90-backend-testing.skill.md +29 -0
- package/template/.trae/skills/README.md +49 -0
- package/template/.vscode/launch.json +38 -0
- package/template/.vscode/settings.json +1 -0
- package/template/Dockerfile +36 -0
- package/template/README.md +229 -0
- package/template/docker-compose.yml +135 -0
- package/template/docs/API_DOCUMENTATION.md +837 -0
- package/template/docs/ARCHITECTURE_REFACTOR.md +109 -0
- package/template/docs/CACHE_MIGRATION_GUIDE.md +142 -0
- package/template/docs/DEPLOYMENT_GUIDE.md +1062 -0
- package/template/docs/DEVELOPMENT_GUIDE.md +1097 -0
- package/template/docs/DOCUMENTATION_CLEANUP_REPORT.md +166 -0
- package/template/docs/DOCUMENTATION_COMPLETION_REPORT.md +223 -0
- package/template/docs/DOCUMENTATION_INDEX.md +294 -0
- package/template/docs/DOCUMENTATION_STRUCTURE.md +221 -0
- package/template/docs/ENTERPRISE_ANNOTATION_SYSTEM_GUIDE.md +2069 -0
- package/template/docs/ENTERPRISE_DATABASE_ARCHITECTURE.md +318 -0
- package/template/docs/ENTERPRISE_DEPLOYMENT_GUIDE.md +547 -0
- package/template/docs/ENTERPRISE_ERROR_HANDLING_GUIDE.md +357 -0
- package/template/docs/ENTERPRISE_LOGGING_SYSTEM_GUIDE.md +494 -0
- package/template/docs/ENVIRONMENT_CONFIG_EXAMPLE.md +69 -0
- package/template/docs/FINAL_IMPLEMENTATION_SUMMARY.md +206 -0
- package/template/docs/HEALTH_CHECK_ROUTE_FIX.md +134 -0
- package/template/docs/IMPLEMENTATION_CHECKLIST.md +204 -0
- package/template/docs/INSTALLATION_GUIDE.md +611 -0
- package/template/docs/INTERCEPTOR_TESTING_REPORT.md +226 -0
- package/template/docs/INTERCEPTOR_TESTING_SCRIPTS.md +143 -0
- package/template/docs/LOGGING_OPTIMIZATION_GUIDE.md +126 -0
- package/template/docs/MEMORY_DATABASE_GUIDE.md +212 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_GUIDE.md +345 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_SUMMARY.md +259 -0
- package/template/docs/NEW_ROUTER_USAGE_GUIDE.md +364 -0
- package/template/docs/QUICK_START.md +268 -0
- package/template/docs/ROUTE_SLASH_COMPATIBILITY_FIX.md +191 -0
- package/template/docs/SERVICE_INTERCEPTOR_GUIDE.md +243 -0
- package/template/docs/SERVICE_LAYER_INDEX.md +205 -0
- package/template/docs/SERVICE_PATTERN_GUIDE.md +270 -0
- package/template/docs/SERVICE_RETURN_VALUE_SPECIFICATION.md +466 -0
- package/template/docs/SWAGGER_DEBUG_MODE_GUIDE.md +80 -0
- package/template/docs/SWAGGER_INTEGRATION_GUIDE.md +416 -0
- package/template/docs/TRANSACTION_MANAGER_USAGE.md +360 -0
- package/template/docs/TROUBLESHOOTING.md +869 -0
- package/template/env.production.example +62 -0
- package/template/jest.config.js +34 -0
- package/template/package-lock.json +13240 -0
- package/template/package.json +119 -0
- package/template/patches/typeorm+0.3.25.patch +22 -0
- package/template/scripts/sync-template.mjs +84 -0
- package/template/scripts/test-annotation-system.sh +48 -0
- package/template/scripts/test-core-functionality.sh +28 -0
- package/template/src/annotations/decorators/ConfigManagement.ts +9 -0
- package/template/src/annotations/decorators/DistributedTracing.ts +9 -0
- package/template/src/annotations/decorators/EnterprisePerformance.ts +9 -0
- package/template/src/annotations/decorators/PerformanceMonitor.ts +32 -0
- package/template/src/annotations/decorators/SecurityAudit.ts +9 -0
- package/template/src/annotations/index.ts +50 -0
- package/template/src/annotations/processors/ConfigManagementProcessor.ts +369 -0
- package/template/src/annotations/processors/DistributedTracingProcessor.ts +288 -0
- package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +189 -0
- package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +101 -0
- package/template/src/annotations/processors/SecurityAuditProcessor.ts +345 -0
- package/template/src/annotations/processors/SwaggerProcessor.ts +612 -0
- package/template/src/annotations/processors/index.ts +10 -0
- package/template/src/app.ts +123 -0
- package/template/src/controllers/base.controller.ts +41 -0
- package/template/src/controllers/cacheManagement.controller.ts +131 -0
- package/template/src/controllers/captcha.controller.ts +57 -0
- package/template/src/controllers/demo/AnnotationDemoController.ts +118 -0
- package/template/src/controllers/example/EnterpriseExampleController.ts +297 -0
- package/template/src/controllers/example/ExampleController.ts +110 -0
- package/template/src/controllers/example/NewAnnotationExampleController.ts +159 -0
- package/template/src/controllers/example/SwaggerExampleController.ts +205 -0
- package/template/src/controllers/example/TransactionExample.controller.ts +336 -0
- package/template/src/controllers/health.controller.ts +235 -0
- package/template/src/controllers/home/register.controller.ts +58 -0
- package/template/src/controllers/home/ytGoods.controller.ts +92 -0
- package/template/src/controllers/home/ytShop.controller.ts +135 -0
- package/template/src/controllers/home/ytUser.controller.ts +89 -0
- package/template/src/controllers/logManagement.controller.ts +396 -0
- package/template/src/controllers/public/emailSend.controller.ts +65 -0
- package/template/src/controllers/public/ytUserAuth.controller.ts +174 -0
- package/template/src/controllers/testData.controller.ts +253 -0
- package/template/src/dto/controller/example/NewAnnotationExampleController.dto.ts +73 -0
- package/template/src/dto/controller/home/emailSend.controller.dto.ts +40 -0
- package/template/src/dto/controller/home/register.controller.dto.ts +45 -0
- package/template/src/dto/controller/home/ytGoods.controller.dto.ts +55 -0
- package/template/src/dto/controller/home/ytShop.controller.dto.ts +69 -0
- package/template/src/dto/controller/home/ytUser.controller.dto.ts +44 -0
- package/template/src/dto/controller/public/ytUserAuth.controller.dto.ts +63 -0
- package/template/src/dto/goods.dto.ts +212 -0
- package/template/src/dto/service/ytService.dto.ts +13 -0
- package/template/src/dto/user.dto.ts +177 -0
- package/template/src/entity/base.entity.ts +13 -0
- package/template/src/entity/columnTypes.ts +13 -0
- package/template/src/entity/goodsImagesUnlockKey.entity.ts +33 -0
- package/template/src/entity/goodsUnlocker.entity.ts +34 -0
- package/template/src/entity/index.ts +15 -0
- package/template/src/entity/shop.entity.ts +52 -0
- package/template/src/entity/shopUser.entity.ts +41 -0
- package/template/src/entity/ytGoods.entity.ts +94 -0
- package/template/src/entity/ytUser.entity.ts +96 -0
- package/template/src/examples/InterceptorExampleRunner.ts +284 -0
- package/template/src/examples/ServiceInterceptorExample.ts +214 -0
- package/template/src/examples/SwaggerProcessorExample.ts +169 -0
- package/template/src/examples/TransactionManagerDemo.ts +377 -0
- package/template/src/examples/cacheExamples.ts +155 -0
- package/template/src/framework/decorator/controller.ts +311 -0
- package/template/src/framework/decorator/processor/AnnotationDecorators.ts +100 -0
- package/template/src/framework/decorator/processor/AnnotationProcessor.ts +156 -0
- package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +45 -0
- package/template/src/framework/decorator/processor/AnnotationRegistry.ts +117 -0
- package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +95 -0
- package/template/src/framework/decorator/processor/ProcessorManager.ts +76 -0
- package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +126 -0
- package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +207 -0
- package/template/src/framework/decorator/refactored/DecoratorFactory.ts +99 -0
- package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +125 -0
- package/template/src/framework/decorator/refactored/DecoratorValidator.ts +128 -0
- package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +139 -0
- package/template/src/framework/decorator/refactored/index.ts +98 -0
- package/template/src/framework/decorator/swagger.ts +150 -0
- package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +375 -0
- package/template/src/framework/interceptors/ServiceCallInterceptor.ts +348 -0
- package/template/src/framework/interceptors/index.ts +19 -0
- package/template/src/framework/plugins/registry.ts +63 -0
- package/template/src/framework/plugins/types.ts +15 -0
- package/template/src/framework/types/ServiceResult.ts +151 -0
- package/template/src/framework/types/index.ts +16 -0
- package/template/src/framework/utils/CacheManager.ts +430 -0
- package/template/src/framework/utils/CacheService.ts +248 -0
- package/template/src/framework/utils/DtoValidator.ts +164 -0
- package/template/src/framework/utils/MigrationHelper.ts +179 -0
- package/template/src/framework/utils/MigrationManager.ts +256 -0
- package/template/src/framework/utils/NewRouter.ts +207 -0
- package/template/src/framework/utils/TransactionManager.ts +172 -0
- package/template/src/framework/utils/bootstrap.ts +445 -0
- package/template/src/framework/utils/cache.ts +269 -0
- package/template/src/framework/utils/databaseConfig.ts +148 -0
- package/template/src/framework/utils/db.ts +39 -0
- package/template/src/framework/utils/dbMonitor.ts +106 -0
- package/template/src/framework/utils/dynamicSwagger.ts +410 -0
- package/template/src/framework/utils/function.ts +61 -0
- package/template/src/framework/utils/gracefulShutdown.ts +131 -0
- package/template/src/framework/utils/logger.ts +388 -0
- package/template/src/framework/utils/metrics.ts +182 -0
- package/template/src/framework/utils/router.ts +417 -0
- package/template/src/framework/utils/swagger.ts +184 -0
- package/template/src/framework/utils/testDb.ts +19 -0
- package/template/src/framework/utils/token.ts +23 -0
- package/template/src/framework/utils/transform.ts +17 -0
- package/template/src/libs/aokEmailSender.ts +42 -0
- package/template/src/libs/captcha.ts +37 -0
- package/template/src/libs/cos.ts +45 -0
- package/template/src/libs/mCache.ts +7 -0
- package/template/src/libs/serviceValidate.ts +3 -0
- package/template/src/libs/tecentSms.ts +51 -0
- package/template/src/middlewares/a.middleware.ts +6 -0
- package/template/src/middlewares/error.middleware.ts +14 -0
- package/template/src/middlewares/logging.middleware.ts +187 -0
- package/template/src/middlewares/static.middleware.ts +79 -0
- package/template/src/middlewares/swagger.middleware.ts +70 -0
- package/template/src/middlewares/token.middleware.ts +32 -0
- package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +172 -0
- package/template/src/migrations/index.ts +6 -0
- package/template/src/plugins/weboffice/core/context.ts +47 -0
- package/template/src/plugins/weboffice/core/errors.ts +51 -0
- package/template/src/plugins/weboffice/core/types.ts +63 -0
- package/template/src/plugins/weboffice/core/utils.ts +7 -0
- package/template/src/plugins/weboffice/entities/index.ts +3 -0
- package/template/src/plugins/weboffice/entities/webofficeFile.entity.ts +28 -0
- package/template/src/plugins/weboffice/entities/webofficeFileVersion.entity.ts +29 -0
- package/template/src/plugins/weboffice/http/routes.ts +179 -0
- package/template/src/plugins/weboffice/index.ts +23 -0
- package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +274 -0
- package/template/src/repository/UserRepository.ts +122 -0
- package/template/src/repository/base/BaseRepository.ts +124 -0
- package/template/src/repository/interfaces/IBaseRepository.ts +67 -0
- package/template/src/routers/index.ts +49 -0
- package/template/src/service/base.service.ts +116 -0
- package/template/src/service/paramValidateTest.service.ts +139 -0
- package/template/src/service/ytGoods.service.ts +42 -0
- package/template/src/service/ytShop.service.ts +90 -0
- package/template/src/service/ytUser.service.ts +451 -0
- package/template/src/test/swaggerParameterTest.ts +90 -0
- package/template/src/utils/testDataInitializer.ts +296 -0
- package/template/static/output.json +15203 -0
- package/template/test/controllers/controllers.test.ts +173 -0
- package/template/test/controllers/example/ExampleController.test.ts +222 -0
- package/template/test/controllers/example/NewAnnotationExampleController.test.ts +200 -0
- package/template/test/framework/TransactionManagerDemo.test.ts +363 -0
- package/template/test/framework/annotation/AnnotationDecorators.test.ts +222 -0
- package/template/test/framework/annotation/AnnotationExecutor.test.ts +246 -0
- package/template/test/framework/annotation/AnnotationProcessor.test.ts +179 -0
- package/template/test/framework/annotation/CustomProcessors.test.ts +313 -0
- package/template/test/framework/annotation/DefaultProcessors.test.ts +371 -0
- package/template/test/framework/annotation/NewRouter.test.ts +272 -0
- package/template/test/framework/annotation/ProcessorManager.test.ts +248 -0
- package/template/test/framework/annotation/setup.ts +26 -0
- package/template/test/framework/cache.test.ts +101 -0
- package/template/test/framework/databaseConfig.test.ts +142 -0
- package/template/test/integration/integration.test.ts +153 -0
- package/template/test/plugins/weboffice/http.routes.int.test.ts +61 -0
- package/template/test/service/business.test.ts +87 -0
- package/template/test/service/paramValidateTest.service.test.ts +184 -0
- package/template/test/service/ytUser.service.test.ts +566 -0
- package/template/test/setup.ts +20 -0
- package/template/test/setupAfterEnv.ts +14 -0
- package/template/test/utils/testHelpers.ts +220 -0
- package/template/test_output.txt +0 -0
- package/template/tsconfig.build.json +17 -0
- package/template/tsconfig.json +31 -0
- package/template/webpack.config.js +71 -0
- package/template/yarn.lock +7354 -0
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
# 企业级日志系统文档
|
|
2
|
+
|
|
3
|
+
## 概述
|
|
4
|
+
|
|
5
|
+
本文档描述了DP-Koa框架的企业级日志系统实现,该系统符合企业项目的要求,提供了完整的日志记录、监控、审计和分析功能。
|
|
6
|
+
|
|
7
|
+
## 系统特性
|
|
8
|
+
|
|
9
|
+
### ✅ 企业级要求满足情况
|
|
10
|
+
|
|
11
|
+
| 要求 | 状态 | 说明 |
|
|
12
|
+
|------|------|------|
|
|
13
|
+
| 结构化日志 | ✅ | JSON格式,包含完整的上下文信息 |
|
|
14
|
+
| 请求追踪 | ✅ | 每个请求分配唯一ID,支持分布式追踪 |
|
|
15
|
+
| 敏感信息脱敏 | ✅ | 自动识别并脱敏敏感字段 |
|
|
16
|
+
| 性能监控 | ✅ | 记录请求耗时、数据库操作等性能指标 |
|
|
17
|
+
| 安全审计 | ✅ | 记录所有安全相关事件和操作 |
|
|
18
|
+
| 日志分类 | ✅ | 按类型分离:结构化、错误、审计、性能 |
|
|
19
|
+
| 日志管理 | ✅ | 提供日志查询、下载、清理等管理功能 |
|
|
20
|
+
| 实时监控 | ✅ | 支持实时日志流和SSE |
|
|
21
|
+
|
|
22
|
+
## 架构设计
|
|
23
|
+
|
|
24
|
+
### 核心组件
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
28
|
+
│ 企业级日志系统架构 │
|
|
29
|
+
├─────────────────────────────────────────────────────────────┤
|
|
30
|
+
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
|
|
31
|
+
│ │ 日志中间件 │ │ 日志器类 │ │ 布局器 │ │
|
|
32
|
+
│ │ │ │ │ │ │ │
|
|
33
|
+
│ │ • 请求追踪 │ │ • 上下文管理 │ │ • JSON格式化 │ │
|
|
34
|
+
│ │ • 性能监控 │ │ • 脱敏处理 │ │ • 敏感信息 │ │
|
|
35
|
+
│ │ • 安全审计 │ │ • 分类记录 │ │ 脱敏 │ │
|
|
36
|
+
│ └─────────────┘ └─────────────┘ └─────────────┘ │
|
|
37
|
+
├─────────────────────────────────────────────────────────────┤
|
|
38
|
+
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
|
|
39
|
+
│ │ 结构化日志 │ │ 错误日志 │ │ 审计日志 │ │
|
|
40
|
+
│ │ │ │ │ │ │ │
|
|
41
|
+
│ │ • 所有日志 │ │ • 错误信息 │ │ • 安全事件 │ │
|
|
42
|
+
│ │ • JSON格式 │ │ • 堆栈跟踪 │ │ • 操作记录 │ │
|
|
43
|
+
│ └─────────────┘ └─────────────┘ └─────────────┘ │
|
|
44
|
+
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
|
|
45
|
+
│ │ 性能日志 │ │ 管理接口 │ │ 实时流 │ │
|
|
46
|
+
│ │ │ │ │ │ │ │
|
|
47
|
+
│ │ • 请求耗时 │ │ • 查询统计 │ │ • SSE流 │ │
|
|
48
|
+
│ │ • 数据库操作 │ │ • 文件下载 │ │ • 实时监控 │ │
|
|
49
|
+
│ └─────────────┘ └─────────────┘ └─────────────┘ │
|
|
50
|
+
└─────────────────────────────────────────────────────────────┘
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## 日志格式
|
|
54
|
+
|
|
55
|
+
### 结构化日志格式
|
|
56
|
+
|
|
57
|
+
```json
|
|
58
|
+
{
|
|
59
|
+
"timestamp": "2025-01-25T10:30:00.000Z",
|
|
60
|
+
"level": "INFO",
|
|
61
|
+
"category": "default",
|
|
62
|
+
"message": "Request completed",
|
|
63
|
+
"service": "dp-koa-framework",
|
|
64
|
+
"version": "1.0.0",
|
|
65
|
+
"environment": "production",
|
|
66
|
+
"requestId": "550e8400-e29b-41d4-a716-446655440000",
|
|
67
|
+
"userId": "user123",
|
|
68
|
+
"sessionId": "session456",
|
|
69
|
+
"traceId": "trace789",
|
|
70
|
+
"spanId": "span101",
|
|
71
|
+
"pid": 12345,
|
|
72
|
+
"hostname": "server-01",
|
|
73
|
+
"method": "GET",
|
|
74
|
+
"url": "/api/users",
|
|
75
|
+
"status": 200,
|
|
76
|
+
"duration": "150ms",
|
|
77
|
+
"responseSize": 1024
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 日志级别
|
|
82
|
+
|
|
83
|
+
| 级别 | 用途 | 示例 |
|
|
84
|
+
|------|------|------|
|
|
85
|
+
| `TRACE` | 详细跟踪信息 | 函数调用、变量值 |
|
|
86
|
+
| `DEBUG` | 调试信息 | 开发环境详细信息 |
|
|
87
|
+
| `INFO` | 一般信息 | 请求处理、业务操作 |
|
|
88
|
+
| `WARN` | 警告信息 | 性能问题、配置问题 |
|
|
89
|
+
| `ERROR` | 错误信息 | 异常、失败操作 |
|
|
90
|
+
| `FATAL` | 致命错误 | 系统崩溃、严重错误 |
|
|
91
|
+
|
|
92
|
+
## 敏感信息脱敏
|
|
93
|
+
|
|
94
|
+
### 自动脱敏字段
|
|
95
|
+
|
|
96
|
+
系统会自动识别并脱敏以下敏感字段:
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
const SENSITIVE_FIELDS = [
|
|
100
|
+
'password', 'token', 'secret', 'key', 'auth', 'credential',
|
|
101
|
+
'ssn', 'socialSecurityNumber', 'creditCard', 'cardNumber',
|
|
102
|
+
'phone', 'email', 'address', 'ip', 'mac'
|
|
103
|
+
]
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### 脱敏示例
|
|
107
|
+
|
|
108
|
+
**原始数据:**
|
|
109
|
+
```json
|
|
110
|
+
{
|
|
111
|
+
"username": "john_doe",
|
|
112
|
+
"password": "secret123",
|
|
113
|
+
"email": "john@example.com",
|
|
114
|
+
"phone": "13800138000"
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**脱敏后:**
|
|
119
|
+
```json
|
|
120
|
+
{
|
|
121
|
+
"username": "john_doe",
|
|
122
|
+
"password": "***REDACTED***",
|
|
123
|
+
"email": "***REDACTED***",
|
|
124
|
+
"phone": "***REDACTED***"
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## 日志分类
|
|
129
|
+
|
|
130
|
+
### 1. 结构化日志 (`/logs/structured/`)
|
|
131
|
+
- **用途**: 所有应用日志的主要存储
|
|
132
|
+
- **格式**: JSON格式
|
|
133
|
+
- **保留**: 30天,50MB/文件
|
|
134
|
+
- **内容**: 请求处理、业务操作、系统状态
|
|
135
|
+
|
|
136
|
+
### 2. 错误日志 (`/logs/error/`)
|
|
137
|
+
- **用途**: 专门记录错误和异常
|
|
138
|
+
- **格式**: JSON格式
|
|
139
|
+
- **保留**: 15天,20MB/文件
|
|
140
|
+
- **内容**: 错误堆栈、异常详情、失败操作
|
|
141
|
+
|
|
142
|
+
### 3. 审计日志 (`/logs/audit/`)
|
|
143
|
+
- **用途**: 安全审计和合规要求
|
|
144
|
+
- **格式**: JSON格式
|
|
145
|
+
- **保留**: 20天,30MB/文件
|
|
146
|
+
- **内容**: 用户操作、权限变更、安全事件
|
|
147
|
+
|
|
148
|
+
### 4. 性能日志 (`/logs/performance/`)
|
|
149
|
+
- **用途**: 性能监控和优化
|
|
150
|
+
- **格式**: JSON格式
|
|
151
|
+
- **保留**: 15天,25MB/文件
|
|
152
|
+
- **内容**: 请求耗时、数据库操作、性能指标
|
|
153
|
+
|
|
154
|
+
## API接口
|
|
155
|
+
|
|
156
|
+
### 日志管理接口
|
|
157
|
+
|
|
158
|
+
#### 1. 获取日志统计
|
|
159
|
+
```http
|
|
160
|
+
GET /admin/logs/stats
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
**响应示例:**
|
|
164
|
+
```json
|
|
165
|
+
{
|
|
166
|
+
"code": 0,
|
|
167
|
+
"data": {
|
|
168
|
+
"totalFiles": 15,
|
|
169
|
+
"totalSize": 1048576,
|
|
170
|
+
"fileTypes": {
|
|
171
|
+
".log": { "count": 12, "size": 800000 },
|
|
172
|
+
".json": { "count": 3, "size": 248576 }
|
|
173
|
+
},
|
|
174
|
+
"lastModified": "2025-01-25T10:30:00.000Z"
|
|
175
|
+
},
|
|
176
|
+
"message": "日志统计信息获取成功"
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
#### 2. 获取日志文件列表
|
|
181
|
+
```http
|
|
182
|
+
GET /admin/logs/files
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
**响应示例:**
|
|
186
|
+
```json
|
|
187
|
+
{
|
|
188
|
+
"code": 0,
|
|
189
|
+
"data": [
|
|
190
|
+
{
|
|
191
|
+
"name": "structured.2025-01-25.log",
|
|
192
|
+
"size": 1024000,
|
|
193
|
+
"modified": "2025-01-25T10:30:00.000Z",
|
|
194
|
+
"created": "2025-01-25T00:00:00.000Z",
|
|
195
|
+
"type": ".log"
|
|
196
|
+
}
|
|
197
|
+
],
|
|
198
|
+
"message": "日志文件列表获取成功"
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
#### 3. 下载日志文件
|
|
203
|
+
```http
|
|
204
|
+
GET /admin/logs/download/{filename}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
#### 4. 查看日志内容
|
|
208
|
+
```http
|
|
209
|
+
GET /admin/logs/view/{filename}?lines=100&offset=0
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
**参数:**
|
|
213
|
+
- `lines`: 返回行数(默认100)
|
|
214
|
+
- `offset`: 起始行号(默认0)
|
|
215
|
+
|
|
216
|
+
#### 5. 清理过期日志
|
|
217
|
+
```http
|
|
218
|
+
POST /admin/logs/clean
|
|
219
|
+
Content-Type: application/json
|
|
220
|
+
|
|
221
|
+
{
|
|
222
|
+
"days": 30
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
#### 6. 设置日志级别
|
|
227
|
+
```http
|
|
228
|
+
POST /admin/logs/level
|
|
229
|
+
Content-Type: application/json
|
|
230
|
+
|
|
231
|
+
{
|
|
232
|
+
"level": "info",
|
|
233
|
+
"category": "default"
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
#### 7. 实时日志流
|
|
238
|
+
```http
|
|
239
|
+
GET /admin/logs/stream/{filename}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
**响应类型**: `text/event-stream`
|
|
243
|
+
|
|
244
|
+
## 使用指南
|
|
245
|
+
|
|
246
|
+
### 1. 基本日志记录
|
|
247
|
+
|
|
248
|
+
```typescript
|
|
249
|
+
import { logger } from '../framework/utils/logger'
|
|
250
|
+
|
|
251
|
+
// 基本日志
|
|
252
|
+
logger.info('用户登录成功', { userId: '123', ip: '192.168.1.1' })
|
|
253
|
+
logger.warn('数据库连接超时', { timeout: 5000 })
|
|
254
|
+
logger.error('文件上传失败', error, { filename: 'test.jpg' })
|
|
255
|
+
|
|
256
|
+
// 业务日志
|
|
257
|
+
logger.business('ORDER_CREATED', {
|
|
258
|
+
orderId: 'ORD-001',
|
|
259
|
+
userId: '123',
|
|
260
|
+
amount: 99.99
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
// 安全审计
|
|
264
|
+
logger.audit('USER_LOGIN', '/api/auth/login', {
|
|
265
|
+
userId: '123',
|
|
266
|
+
ip: '192.168.1.1',
|
|
267
|
+
userAgent: 'Mozilla/5.0...'
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
// 性能监控
|
|
271
|
+
logger.performance('DATABASE_QUERY', 150, {
|
|
272
|
+
query: 'SELECT * FROM users',
|
|
273
|
+
rows: 100
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
// 安全事件
|
|
277
|
+
logger.security('FAILED_LOGIN', 'medium', {
|
|
278
|
+
username: 'admin',
|
|
279
|
+
ip: '192.168.1.1',
|
|
280
|
+
attempts: 3
|
|
281
|
+
})
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### 2. 请求上下文日志
|
|
285
|
+
|
|
286
|
+
```typescript
|
|
287
|
+
// 在控制器中使用
|
|
288
|
+
export class UserController extends BaseController {
|
|
289
|
+
async getUser(ctx: Context) {
|
|
290
|
+
// 自动包含请求ID和用户信息
|
|
291
|
+
ctx.logger.info('获取用户信息', { userId: ctx.params.id })
|
|
292
|
+
|
|
293
|
+
try {
|
|
294
|
+
const user = await this.userService.findById(ctx.params.id)
|
|
295
|
+
ctx.logger.business('USER_VIEWED', { userId: ctx.params.id })
|
|
296
|
+
|
|
297
|
+
ctx.body = { code: 0, data: user }
|
|
298
|
+
} catch (error) {
|
|
299
|
+
ctx.logger.error('获取用户失败', error, { userId: ctx.params.id })
|
|
300
|
+
throw error
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
### 3. 自定义日志器
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
import { createRequestLogger } from '../framework/utils/logger'
|
|
310
|
+
|
|
311
|
+
// 创建带请求ID的日志器
|
|
312
|
+
const requestLogger = createRequestLogger('req-123')
|
|
313
|
+
requestLogger.setRequestContext('req-123', 'user456', 'session789')
|
|
314
|
+
|
|
315
|
+
// 使用自定义日志器
|
|
316
|
+
requestLogger.info('处理用户请求', { action: 'update_profile' })
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
## 配置说明
|
|
320
|
+
|
|
321
|
+
### 环境变量
|
|
322
|
+
|
|
323
|
+
```bash
|
|
324
|
+
# 日志级别
|
|
325
|
+
LOG_LEVEL=info
|
|
326
|
+
|
|
327
|
+
# 日志目录
|
|
328
|
+
LOG_DIR=./logs
|
|
329
|
+
|
|
330
|
+
# 服务名称
|
|
331
|
+
SERVICE_NAME=dp-koa-framework
|
|
332
|
+
|
|
333
|
+
# 服务版本
|
|
334
|
+
SERVICE_VERSION=1.0.0
|
|
335
|
+
|
|
336
|
+
# 环境
|
|
337
|
+
NODE_ENV=production
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
### 日志配置
|
|
341
|
+
|
|
342
|
+
```typescript
|
|
343
|
+
// src/framework/utils/logger.ts
|
|
344
|
+
log4js.configure({
|
|
345
|
+
pm2: true,
|
|
346
|
+
appenders: {
|
|
347
|
+
structured: {
|
|
348
|
+
type: 'dateFile',
|
|
349
|
+
filename: getFullPath("/logs/structured"),
|
|
350
|
+
pattern: 'yyyy-MM-dd.log',
|
|
351
|
+
maxLogSize: '50M',
|
|
352
|
+
backups: 30
|
|
353
|
+
},
|
|
354
|
+
// ... 其他配置
|
|
355
|
+
},
|
|
356
|
+
categories: {
|
|
357
|
+
default: {
|
|
358
|
+
appenders: ['console', 'structured'],
|
|
359
|
+
level: 'debug'
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
})
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
## 监控和告警
|
|
366
|
+
|
|
367
|
+
### 1. 性能监控
|
|
368
|
+
|
|
369
|
+
系统自动记录以下性能指标:
|
|
370
|
+
- HTTP请求耗时
|
|
371
|
+
- 数据库查询时间
|
|
372
|
+
- 缓存操作耗时
|
|
373
|
+
- 内存使用情况
|
|
374
|
+
|
|
375
|
+
### 2. 错误监控
|
|
376
|
+
|
|
377
|
+
自动记录:
|
|
378
|
+
- 未捕获异常
|
|
379
|
+
- 数据库连接错误
|
|
380
|
+
- 第三方服务调用失败
|
|
381
|
+
- 业务逻辑错误
|
|
382
|
+
|
|
383
|
+
### 3. 安全监控
|
|
384
|
+
|
|
385
|
+
记录安全事件:
|
|
386
|
+
- 登录失败
|
|
387
|
+
- 权限越界
|
|
388
|
+
- 异常访问模式
|
|
389
|
+
- 敏感操作
|
|
390
|
+
|
|
391
|
+
## 最佳实践
|
|
392
|
+
|
|
393
|
+
### 1. 日志记录原则
|
|
394
|
+
|
|
395
|
+
- **结构化**: 使用JSON格式,便于解析和分析
|
|
396
|
+
- **上下文**: 包含足够的上下文信息
|
|
397
|
+
- **级别**: 合理使用日志级别
|
|
398
|
+
- **性能**: 避免在热路径中记录过多日志
|
|
399
|
+
|
|
400
|
+
### 2. 敏感信息处理
|
|
401
|
+
|
|
402
|
+
- 自动脱敏敏感字段
|
|
403
|
+
- 避免记录完整密码或令牌
|
|
404
|
+
- 记录操作而非数据内容
|
|
405
|
+
|
|
406
|
+
### 3. 性能考虑
|
|
407
|
+
|
|
408
|
+
- 异步写入日志
|
|
409
|
+
- 批量处理日志
|
|
410
|
+
- 合理设置日志级别
|
|
411
|
+
- 定期清理过期日志
|
|
412
|
+
|
|
413
|
+
### 4. 安全考虑
|
|
414
|
+
|
|
415
|
+
- 日志文件权限控制
|
|
416
|
+
- 传输加密
|
|
417
|
+
- 访问审计
|
|
418
|
+
- 备份策略
|
|
419
|
+
|
|
420
|
+
## 故障排查
|
|
421
|
+
|
|
422
|
+
### 常见问题
|
|
423
|
+
|
|
424
|
+
1. **日志文件过大**
|
|
425
|
+
- 检查日志级别设置
|
|
426
|
+
- 调整文件大小限制
|
|
427
|
+
- 增加日志轮转频率
|
|
428
|
+
|
|
429
|
+
2. **性能影响**
|
|
430
|
+
- 降低日志级别
|
|
431
|
+
- 使用异步写入
|
|
432
|
+
- 优化日志格式
|
|
433
|
+
|
|
434
|
+
3. **磁盘空间不足**
|
|
435
|
+
- 设置自动清理
|
|
436
|
+
- 压缩历史日志
|
|
437
|
+
- 调整保留策略
|
|
438
|
+
|
|
439
|
+
### 调试技巧
|
|
440
|
+
|
|
441
|
+
1. **查看实时日志**
|
|
442
|
+
```bash
|
|
443
|
+
tail -f logs/structured.2025-01-25.log
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
2. **搜索特定请求**
|
|
447
|
+
```bash
|
|
448
|
+
grep "requestId:550e8400" logs/structured.2025-01-25.log
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
3. **分析性能问题**
|
|
452
|
+
```bash
|
|
453
|
+
grep "PERFORMANCE" logs/performance.2025-01-25.log | jq '.duration'
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
## 扩展功能
|
|
457
|
+
|
|
458
|
+
### 1. 日志聚合
|
|
459
|
+
|
|
460
|
+
可以集成ELK Stack:
|
|
461
|
+
- **Elasticsearch**: 日志存储和搜索
|
|
462
|
+
- **Logstash**: 日志收集和处理
|
|
463
|
+
- **Kibana**: 日志可视化和分析
|
|
464
|
+
|
|
465
|
+
### 2. 告警系统
|
|
466
|
+
|
|
467
|
+
集成告警功能:
|
|
468
|
+
- 错误率阈值告警
|
|
469
|
+
- 性能指标告警
|
|
470
|
+
- 安全事件告警
|
|
471
|
+
|
|
472
|
+
### 3. 日志分析
|
|
473
|
+
|
|
474
|
+
支持高级分析:
|
|
475
|
+
- 用户行为分析
|
|
476
|
+
- 性能趋势分析
|
|
477
|
+
- 安全威胁检测
|
|
478
|
+
|
|
479
|
+
## 总结
|
|
480
|
+
|
|
481
|
+
DP-Koa框架的企业级日志系统提供了完整的日志记录、监控、审计和分析功能,满足企业项目的各种需求:
|
|
482
|
+
|
|
483
|
+
- ✅ **结构化日志**: JSON格式,便于解析和分析
|
|
484
|
+
- ✅ **请求追踪**: 完整的请求链路追踪
|
|
485
|
+
- ✅ **敏感信息脱敏**: 自动识别和脱敏敏感数据
|
|
486
|
+
- ✅ **性能监控**: 详细的性能指标记录
|
|
487
|
+
- ✅ **安全审计**: 完整的安全事件记录
|
|
488
|
+
- ✅ **日志管理**: 丰富的管理接口和功能
|
|
489
|
+
- ✅ **实时监控**: 支持实时日志流
|
|
490
|
+
- ✅ **高可用性**: 异步写入,不影响业务性能
|
|
491
|
+
|
|
492
|
+
该系统为企业级应用提供了可靠的日志基础设施,支持运维监控、安全审计、性能优化和故障排查等各个方面。
|
|
493
|
+
|
|
494
|
+
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# 环境配置文件示例
|
|
2
|
+
|
|
3
|
+
## 开发环境 (.env.development)
|
|
4
|
+
```
|
|
5
|
+
NODE_ENV=development
|
|
6
|
+
|
|
7
|
+
# 数据库配置
|
|
8
|
+
db_enable=1
|
|
9
|
+
db_host=127.0.0.1
|
|
10
|
+
db_port=3306
|
|
11
|
+
db_username=root
|
|
12
|
+
db_password=123456
|
|
13
|
+
db_database=ytkj_service
|
|
14
|
+
db_memory=0
|
|
15
|
+
|
|
16
|
+
# 新注解系统配置
|
|
17
|
+
USE_NEW_ANNOTATION_SYSTEM=1
|
|
18
|
+
|
|
19
|
+
# 日志配置
|
|
20
|
+
LOG_LEVEL=info
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 生产环境 (.env.production)
|
|
24
|
+
```
|
|
25
|
+
NODE_ENV=production
|
|
26
|
+
|
|
27
|
+
# 数据库配置
|
|
28
|
+
db_enable=1
|
|
29
|
+
db_host=your-production-host
|
|
30
|
+
db_port=3306
|
|
31
|
+
db_username=your-username
|
|
32
|
+
db_password=your-password
|
|
33
|
+
db_database=ytkj_service_prod
|
|
34
|
+
db_memory=0
|
|
35
|
+
|
|
36
|
+
# 新注解系统配置(生产环境建议先禁用)
|
|
37
|
+
USE_NEW_ANNOTATION_SYSTEM=0
|
|
38
|
+
|
|
39
|
+
# 日志配置
|
|
40
|
+
LOG_LEVEL=warn
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## 测试环境 (.env.test)
|
|
44
|
+
```
|
|
45
|
+
NODE_ENV=test
|
|
46
|
+
|
|
47
|
+
# 数据库配置
|
|
48
|
+
db_enable=1
|
|
49
|
+
db_memory=1
|
|
50
|
+
|
|
51
|
+
# 新注解系统配置
|
|
52
|
+
USE_NEW_ANNOTATION_SYSTEM=1
|
|
53
|
+
|
|
54
|
+
# 日志配置
|
|
55
|
+
LOG_LEVEL=error
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## 配置说明
|
|
59
|
+
|
|
60
|
+
### USE_NEW_ANNOTATION_SYSTEM
|
|
61
|
+
- `1`: 启用新注解处理器系统
|
|
62
|
+
- `0`: 使用旧注解系统(默认)
|
|
63
|
+
|
|
64
|
+
### 迁移建议
|
|
65
|
+
1. 开发环境:启用新系统进行测试
|
|
66
|
+
2. 测试环境:启用新系统进行验证
|
|
67
|
+
3. 生产环境:先禁用,逐步迁移后启用
|
|
68
|
+
|
|
69
|
+
|