@vafast/request-logger 0.4.0 → 0.4.2
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 +276 -22
- package/dist/index.d.mts +20 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +86 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -13,18 +13,25 @@ npm install @vafast/request-logger
|
|
|
13
13
|
```typescript
|
|
14
14
|
import { requestLogger } from '@vafast/request-logger'
|
|
15
15
|
|
|
16
|
+
// 最简配置 - 开箱即用
|
|
16
17
|
server.use(requestLogger({
|
|
17
18
|
url: 'http://log-server:9005/api/logs/ingest',
|
|
18
19
|
service: 'my-service',
|
|
19
|
-
headers: { Authorization: 'Bearer apiKeyId:apiKeySecret' },
|
|
20
|
-
onError: (err) => console.error('日志记录失败', err),
|
|
21
20
|
}))
|
|
22
21
|
```
|
|
23
22
|
|
|
24
|
-
|
|
23
|
+
**开箱即用特性**:
|
|
24
|
+
- ✅ stdout 双写(K8s 友好)
|
|
25
|
+
- ✅ 智能日志级别(2xx→INFO,4xx→WARN,5xx→ERROR)
|
|
26
|
+
- ✅ 默认排除 `/health`、`/metrics` 等
|
|
27
|
+
- ✅ 自动提取客户端 IP
|
|
28
|
+
- ✅ 自动读取 Request ID
|
|
29
|
+
- ✅ 智能错误处理(节流 + 结构化输出)
|
|
25
30
|
|
|
26
31
|
## 配置
|
|
27
32
|
|
|
33
|
+
### 基础配置
|
|
34
|
+
|
|
28
35
|
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|
|
29
36
|
|------|------|------|--------|------|
|
|
30
37
|
| `url` | `string` | 是 | - | 日志服务 URL |
|
|
@@ -32,10 +39,161 @@ server.use(requestLogger({
|
|
|
32
39
|
| `headers` | `Record<string, string>` | 否 | `{}` | 自定义请求头(如认证) |
|
|
33
40
|
| `timeout` | `number` | 否 | `5000` | 超时时间(毫秒) |
|
|
34
41
|
| `sanitize` | `SanitizeConfig` | 否 | - | 敏感数据清洗配置 |
|
|
35
|
-
| `onError` | `(err) => void` | 否 |
|
|
42
|
+
| `onError` | `(err, ctx) => void` | 否 | 内置智能处理 | 错误回调,`ctx.droppedCount` 为被节流忽略的错误数 |
|
|
36
43
|
| `enabled` | `boolean` | 否 | `true` | 是否启用 |
|
|
44
|
+
| `excludePaths` | `(string \| RegExp)[]` | 否 | `[]` | 排除的路径列表(在默认排除基础上追加) |
|
|
45
|
+
| `useDefaultExcludePaths` | `boolean` | 否 | `true` | 是否使用默认排除路径 |
|
|
46
|
+
| `sampleRate` | `number` | 否 | `1` | 日志采样率 (0-1),1 = 全部,0.1 = 10% |
|
|
47
|
+
| `requestIdHeader` | `string` | 否 | `'x-request-id'` | Request ID 的 header 名称 |
|
|
48
|
+
|
|
49
|
+
### 默认排除路径
|
|
50
|
+
|
|
51
|
+
以下路径默认不记录日志(可通过 `useDefaultExcludePaths: false` 关闭):
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
/health, /healthz, /ready, /readiness, /liveness, /metrics, /favicon.ico
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### 熔断器配置 (Circuit Breaker)
|
|
58
|
+
|
|
59
|
+
当日志服务不可用时,避免无谓的超时等待。
|
|
60
|
+
|
|
61
|
+
| 参数 | 类型 | 默认值 | 说明 |
|
|
62
|
+
|------|------|--------|------|
|
|
63
|
+
| `circuitBreaker.failureThreshold` | `number` | `5` | 触发熔断的连续失败次数 |
|
|
64
|
+
| `circuitBreaker.resetTimeout` | `number` | `60000` | 熔断恢复时间(毫秒) |
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
requestLogger({
|
|
68
|
+
url: '...',
|
|
69
|
+
service: '...',
|
|
70
|
+
circuitBreaker: {
|
|
71
|
+
failureThreshold: 5, // 连续失败 5 次后熔断
|
|
72
|
+
resetTimeout: 60000, // 1 分钟后尝试恢复
|
|
73
|
+
},
|
|
74
|
+
})
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**工作原理**:
|
|
78
|
+
1. 正常状态:每个请求都尝试上报
|
|
79
|
+
2. 连续失败达到阈值:进入熔断状态,跳过所有上报
|
|
80
|
+
3. 熔断时间到期:进入半开状态,允许一个请求通过测试
|
|
81
|
+
4. 测试成功:恢复正常;测试失败:继续熔断
|
|
82
|
+
|
|
83
|
+
### stdout 双写配置 (Dual Write)
|
|
84
|
+
|
|
85
|
+
同时输出到 stdout,用于 K8s 日志采集(如 TKE + CLS)。即使 log-server 挂了,运维也能从 CLS 查日志。
|
|
86
|
+
|
|
87
|
+
| 参数 | 类型 | 默认值 | 说明 |
|
|
88
|
+
|------|------|--------|------|
|
|
89
|
+
| `stdout.enabled` | `boolean` | `true` | 是否启用 stdout 输出 |
|
|
90
|
+
| `stdout.format` | `'json' \| 'text'` | `'json'` | 输出格式 |
|
|
91
|
+
| `stdout.includeBody` | `boolean` | `true` | 是否包含请求体(已脱敏) |
|
|
92
|
+
| `stdout.includeResponse` | `boolean` | `false` | 是否包含响应体(可能很大) |
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
requestLogger({
|
|
96
|
+
url: 'http://log-server:9005/api/logs/ingest',
|
|
97
|
+
service: 'auth-server',
|
|
98
|
+
stdout: {
|
|
99
|
+
enabled: true, // 启用双写
|
|
100
|
+
format: 'json', // JSON 格式(K8s 友好)
|
|
101
|
+
// includeBody: true, // 默认包含请求体(已脱敏)
|
|
102
|
+
// includeResponse: false, // 默认不含响应体(可能很大)
|
|
103
|
+
},
|
|
104
|
+
})
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
**stdout 输出格式**(精简版,兼容 pino/K8s):
|
|
108
|
+
|
|
109
|
+
```json
|
|
110
|
+
{"level":30,"time":1706123456789,"service":"auth-server","method":"POST","path":"/api/users","status":200,"duration":50,"requestId":"abc-123","clientIp":"1.2.3.4","msg":"POST /api/users 200 50ms"}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
**日志级别根据状态码自动设置**:
|
|
114
|
+
|
|
115
|
+
| 状态码 | 级别 | pino level |
|
|
116
|
+
|--------|------|------------|
|
|
117
|
+
| 2xx | INFO | 30 |
|
|
118
|
+
| 4xx | WARN | 40 |
|
|
119
|
+
| 5xx | ERROR | 50 |
|
|
120
|
+
|
|
121
|
+
**架构图**:
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
请求进来
|
|
125
|
+
│
|
|
126
|
+
▼
|
|
127
|
+
requestLogger 中间件
|
|
128
|
+
│
|
|
129
|
+
├── stdout(JSON)──▶ K8s 采集 ──▶ CLS/Loki(运维备份)
|
|
130
|
+
│
|
|
131
|
+
└── HTTP 推送 ──▶ log-server ──▶ MongoDB ──▶ ones(用户查询)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 错误节流配置 (Error Throttle)
|
|
135
|
+
|
|
136
|
+
避免相同错误刷屏,在一段时间内只打印一次。
|
|
137
|
+
|
|
138
|
+
| 参数 | 类型 | 默认值 | 说明 |
|
|
139
|
+
|------|------|--------|------|
|
|
140
|
+
| `errorThrottle.interval` | `number` | `60000` | 节流间隔(毫秒) |
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
requestLogger({
|
|
144
|
+
url: '...',
|
|
145
|
+
service: '...',
|
|
146
|
+
errorThrottle: {
|
|
147
|
+
interval: 60000, // 同类错误 1 分钟内只打 1 条
|
|
148
|
+
},
|
|
149
|
+
onError: (err, { droppedCount }) => {
|
|
150
|
+
// droppedCount: 上次打印到这次之间被忽略的错误数
|
|
151
|
+
logger.warn(
|
|
152
|
+
{ errorName: err.name, errorMessage: err.message, droppedCount },
|
|
153
|
+
droppedCount > 0
|
|
154
|
+
? `日志上报失败 (已忽略 ${droppedCount} 条)`
|
|
155
|
+
: '日志上报失败'
|
|
156
|
+
)
|
|
157
|
+
},
|
|
158
|
+
})
|
|
159
|
+
```
|
|
37
160
|
|
|
38
|
-
|
|
161
|
+
**效果对比**:
|
|
162
|
+
|
|
163
|
+
```
|
|
164
|
+
# 之前(日志服务挂了)
|
|
165
|
+
日志上报失败
|
|
166
|
+
日志上报失败
|
|
167
|
+
日志上报失败
|
|
168
|
+
... (每秒好几条,刷屏)
|
|
169
|
+
|
|
170
|
+
# 之后
|
|
171
|
+
日志上报失败
|
|
172
|
+
(沉默 1 分钟)
|
|
173
|
+
日志上报失败 (已忽略 120 条)
|
|
174
|
+
(沉默 1 分钟)
|
|
175
|
+
日志上报失败 (已忽略 118 条)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## 路径排除
|
|
179
|
+
|
|
180
|
+
### excludePaths 配置
|
|
181
|
+
|
|
182
|
+
在中间件配置中排除特定路径:
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
requestLogger({
|
|
186
|
+
url: '...',
|
|
187
|
+
service: '...',
|
|
188
|
+
excludePaths: [
|
|
189
|
+
'/health', // 精确匹配
|
|
190
|
+
'/internal/', // 前缀匹配(含子路径)
|
|
191
|
+
/^\/metrics/, // 正则匹配
|
|
192
|
+
],
|
|
193
|
+
})
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### 路由级别控制
|
|
39
197
|
|
|
40
198
|
在路由定义中设置 `log: false` 跳过日志记录:
|
|
41
199
|
|
|
@@ -69,22 +227,38 @@ server.use(requestLogger({
|
|
|
69
227
|
status: 200,
|
|
70
228
|
duration: 50,
|
|
71
229
|
service: 'my-service',
|
|
72
|
-
userId: 'user123',
|
|
73
|
-
appId: 'app456',
|
|
74
|
-
authType: 'jwt',
|
|
75
|
-
ip: '192.168.1.1',
|
|
76
|
-
userAgent: 'Mozilla/5.0...',
|
|
77
|
-
traceId: 'trace789',
|
|
78
230
|
createdAt: '2024-01-01T00:00:00.000Z',
|
|
79
|
-
response: {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
code: 0,
|
|
83
|
-
},
|
|
84
|
-
responseData: { ... },
|
|
231
|
+
response: { success: true, message: 'OK' },
|
|
232
|
+
clientIp: '1.2.3.4', // 可选:从 X-Forwarded-For 等提取
|
|
233
|
+
requestId: 'abc-123-def-456', // 可选:分布式追踪 ID
|
|
85
234
|
}
|
|
86
235
|
```
|
|
87
236
|
|
|
237
|
+
### 客户端 IP 提取
|
|
238
|
+
|
|
239
|
+
自动从以下 header 提取(按优先级):
|
|
240
|
+
|
|
241
|
+
1. `X-Forwarded-For`(第一个 IP)
|
|
242
|
+
2. `X-Real-IP`
|
|
243
|
+
3. `CF-Connecting-IP`(Cloudflare)
|
|
244
|
+
4. `True-Client-IP`(Akamai)
|
|
245
|
+
|
|
246
|
+
### Request ID 支持
|
|
247
|
+
|
|
248
|
+
支持分布式追踪,自动从以下位置获取:
|
|
249
|
+
|
|
250
|
+
1. `req.id`(如果使用了 `@vafast/request-id` 中间件)
|
|
251
|
+
2. 指定的 header(默认 `x-request-id`)
|
|
252
|
+
|
|
253
|
+
```typescript
|
|
254
|
+
import { requestId } from '@vafast/request-id'
|
|
255
|
+
import { requestLogger } from '@vafast/request-logger'
|
|
256
|
+
|
|
257
|
+
// 推荐:配合 request-id 中间件使用
|
|
258
|
+
app.use(requestId()) // 先生成/读取 ID
|
|
259
|
+
app.use(requestLogger({ ... })) // 自动读取 req.id
|
|
260
|
+
```
|
|
261
|
+
|
|
88
262
|
## 敏感数据脱敏
|
|
89
263
|
|
|
90
264
|
默认自动脱敏以下字段:
|
|
@@ -109,8 +283,88 @@ requestLogger({
|
|
|
109
283
|
|
|
110
284
|
## 特性
|
|
111
285
|
|
|
112
|
-
-
|
|
113
|
-
-
|
|
114
|
-
-
|
|
115
|
-
-
|
|
116
|
-
-
|
|
286
|
+
- **异步非阻塞**:不影响响应速度
|
|
287
|
+
- **stdout 双写**:同时输出到 stdout,支持 K8s 日志采集
|
|
288
|
+
- **智能日志级别**:根据状态码自动设置 INFO/WARN/ERROR
|
|
289
|
+
- **熔断器**:日志服务故障时自动熔断,避免雪崩
|
|
290
|
+
- **错误节流**:相同错误不刷屏,带统计计数
|
|
291
|
+
- **默认排除健康检查**:`/health`、`/metrics` 等路径默认不记录
|
|
292
|
+
- **路径排除**:支持精确匹配、前缀匹配、正则匹配
|
|
293
|
+
- **日志采样**:高流量场景下只记录部分请求
|
|
294
|
+
- **客户端 IP 提取**:自动从 X-Forwarded-For 等获取真实 IP
|
|
295
|
+
- **Request ID 支持**:分布式追踪,兼容 `@vafast/request-id`
|
|
296
|
+
- **敏感数据脱敏**:自动清洗密码、Token 等敏感字段
|
|
297
|
+
- **路由级别控制**:可在路由定义中禁用日志
|
|
298
|
+
- **支持多租户**:通过 headers 传递 appId
|
|
299
|
+
|
|
300
|
+
## 完整示例
|
|
301
|
+
|
|
302
|
+
### 最简配置(推荐)
|
|
303
|
+
|
|
304
|
+
```typescript
|
|
305
|
+
import { requestId } from '@vafast/request-id'
|
|
306
|
+
import { requestLogger } from '@vafast/request-logger'
|
|
307
|
+
|
|
308
|
+
// 推荐配合 request-id 使用
|
|
309
|
+
app.use(requestId())
|
|
310
|
+
app.use(requestLogger({
|
|
311
|
+
url: 'http://log-server:9005/api/logs/ingest',
|
|
312
|
+
service: 'auth-server',
|
|
313
|
+
}))
|
|
314
|
+
|
|
315
|
+
// 开箱即用:
|
|
316
|
+
// ✅ stdout 双写默认开启
|
|
317
|
+
// ✅ 智能日志级别 (2xx/4xx/5xx)
|
|
318
|
+
// ✅ 健康检查路径默认排除
|
|
319
|
+
// ✅ 客户端 IP 自动提取
|
|
320
|
+
// ✅ Request ID 自动读取
|
|
321
|
+
// ✅ 智能 onError 处理
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
### 完整配置
|
|
325
|
+
|
|
326
|
+
```typescript
|
|
327
|
+
import { requestLogger } from '@vafast/request-logger'
|
|
328
|
+
import { logger } from './logger'
|
|
329
|
+
|
|
330
|
+
server.use(requestLogger({
|
|
331
|
+
url: 'http://log-server:9005/api/logs/ingest',
|
|
332
|
+
service: 'auth-server',
|
|
333
|
+
headers: { Authorization: 'Bearer ak_xxx:sk_xxx' },
|
|
334
|
+
timeout: 5000,
|
|
335
|
+
enabled: true,
|
|
336
|
+
// 路径排除(追加到默认排除列表)
|
|
337
|
+
excludePaths: ['/verifyApiKey'],
|
|
338
|
+
useDefaultExcludePaths: true, // 使用默认排除(/health 等)
|
|
339
|
+
// 日志采样(高流量场景)
|
|
340
|
+
sampleRate: 1, // 1 = 100%,0.1 = 10%
|
|
341
|
+
// Request ID header
|
|
342
|
+
requestIdHeader: 'x-request-id',
|
|
343
|
+
// stdout 双写(K8s 日志采集)
|
|
344
|
+
stdout: {
|
|
345
|
+
enabled: true,
|
|
346
|
+
format: 'json',
|
|
347
|
+
},
|
|
348
|
+
// 熔断器
|
|
349
|
+
circuitBreaker: {
|
|
350
|
+
failureThreshold: 5,
|
|
351
|
+
resetTimeout: 60000,
|
|
352
|
+
},
|
|
353
|
+
// 错误节流
|
|
354
|
+
errorThrottle: {
|
|
355
|
+
interval: 60000,
|
|
356
|
+
},
|
|
357
|
+
// 自定义错误处理(可选,默认已有智能处理)
|
|
358
|
+
onError: (err: Error, { droppedCount }: { droppedCount: number }) =>
|
|
359
|
+
logger.warn(
|
|
360
|
+
{
|
|
361
|
+
errorName: err.name,
|
|
362
|
+
errorMessage: err.message,
|
|
363
|
+
droppedCount,
|
|
364
|
+
},
|
|
365
|
+
droppedCount > 0
|
|
366
|
+
? `request-logger 上报失败 (已忽略 ${droppedCount} 条相同错误)`
|
|
367
|
+
: 'request-logger 上报失败'
|
|
368
|
+
),
|
|
369
|
+
}))
|
|
370
|
+
```
|
package/dist/index.d.mts
CHANGED
|
@@ -79,6 +79,17 @@ interface ErrorThrottleConfig {
|
|
|
79
79
|
/** 同类错误的节流间隔(毫秒),默认 60000(1分钟) */
|
|
80
80
|
interval?: number;
|
|
81
81
|
}
|
|
82
|
+
/** stdout 双写配置 */
|
|
83
|
+
interface StdoutConfig {
|
|
84
|
+
/** 是否启用 stdout 输出 @default true */
|
|
85
|
+
enabled?: boolean;
|
|
86
|
+
/** 输出格式 @default 'json' */
|
|
87
|
+
format?: 'json' | 'text';
|
|
88
|
+
/** 是否包含请求体 @default true */
|
|
89
|
+
includeBody?: boolean;
|
|
90
|
+
/** 是否包含响应体(可能很大)@default false */
|
|
91
|
+
includeResponse?: boolean;
|
|
92
|
+
}
|
|
82
93
|
/** 请求日志配置 */
|
|
83
94
|
interface RequestLoggerOptions {
|
|
84
95
|
/** 日志服务 URL */
|
|
@@ -99,10 +110,18 @@ interface RequestLoggerOptions {
|
|
|
99
110
|
enabled?: boolean;
|
|
100
111
|
/** 排除的路径列表(精确匹配或正则),这些路径不记录日志 */
|
|
101
112
|
excludePaths?: (string | RegExp)[];
|
|
113
|
+
/** 是否使用默认排除路径(/health, /metrics 等)@default true */
|
|
114
|
+
useDefaultExcludePaths?: boolean;
|
|
102
115
|
/** 熔断器配置 */
|
|
103
116
|
circuitBreaker?: CircuitBreakerConfig;
|
|
104
117
|
/** 错误节流配置 */
|
|
105
118
|
errorThrottle?: ErrorThrottleConfig;
|
|
119
|
+
/** stdout 双写配置(用于 K8s 日志采集) */
|
|
120
|
+
stdout?: StdoutConfig;
|
|
121
|
+
/** 日志采样率 (0-1),1 表示记录所有请求,0.1 表示只记录 10% @default 1 */
|
|
122
|
+
sampleRate?: number;
|
|
123
|
+
/** 请求 ID 的 header 名称,用于分布式追踪 @default 'x-request-id' */
|
|
124
|
+
requestIdHeader?: string;
|
|
106
125
|
}
|
|
107
126
|
/**
|
|
108
127
|
* 请求日志中间件
|
|
@@ -122,5 +141,5 @@ declare function requestLogger(options: RequestLoggerOptions): vafast0.TypedMidd
|
|
|
122
141
|
/** @deprecated 使用 requestLogger 代替 */
|
|
123
142
|
declare const createRequestLogger: typeof requestLogger;
|
|
124
143
|
//#endregion
|
|
125
|
-
export { CircuitBreakerConfig, ErrorThrottleConfig, LogData, RequestData, RequestLoggerOptions, ResponseData, type SanitizeConfig, createRequestLogger, requestLogger as default, requestLogger, sanitize, sanitizeHeaders };
|
|
144
|
+
export { CircuitBreakerConfig, ErrorThrottleConfig, LogData, RequestData, RequestLoggerOptions, ResponseData, type SanitizeConfig, StdoutConfig, createRequestLogger, requestLogger as default, requestLogger, sanitize, sanitizeHeaders };
|
|
126
145
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/sanitize.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;AAQA;AA+DA;AAAkC,UA/DjB,cAAA,CA+DiB;EAAY;EAA4B,YAAA,CAAA,EAAA,MAAA,EAAA;EAAC;EA+D3D,UAAA,CAAA,EAAA,MAAe,EAAA;EACpB;EACA,WAAA,CAAA,EAAA,MAAA;EACR;EAAM,QAAA,CAAA,EAAA,MAAA;;;;AC7GT;;;;;AAoBA;AAGA;AAMA;AAQiB,iBDMD,QCNoB,CAAA,CAAA,CAAA,CAAA,IAAA,EDMF,CCNE,EAAA,MAAA,CAAA,EDMU,cCNV,EAAA,KAAA,CAAA,EAAA,MAAA,CAAA,EDMsC,CCNtC;AAMpC
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/sanitize.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;AAQA;AA+DA;AAAkC,UA/DjB,cAAA,CA+DiB;EAAY;EAA4B,YAAA,CAAA,EAAA,MAAA,EAAA;EAAC;EA+D3D,UAAA,CAAA,EAAA,MAAe,EAAA;EACpB;EACA,WAAA,CAAA,EAAA,MAAA;EACR;EAAM,QAAA,CAAA,EAAA,MAAA;;;;AC7GT;;;;;AAoBA;AAGA;AAMA;AAQiB,iBDMD,QCNoB,CAAA,CAAA,CAAA,CAAA,IAAA,EDMF,CCNE,EAAA,MAAA,CAAA,EDMU,cCNV,EAAA,KAAA,CAAA,EAAA,MAAA,CAAA,EDMsC,CCNtC;AAMpC;AAuBA;;;;;;;;;AAyJgB,iBDjHA,eAAA,CCiHuB,OAAA,EDhH5B,MCgHgD,CAAA,MAAA,EAAA,MAAA,CAAA,EAAA,MAAA,CAAA,ED/GhD,cC+GgD,CAAA,ED9GxD,MC8GwD,CAAA,MAAA,EAAA,MAAA,CAAA;;;;UA3N1C,WAAA;EDpBA,MAAA,EAAA,MAAA;EA+DD,GAAA,EAAA,MAAQ;EAAU,IAAA,EAAA,MAAA;EAAY,OAAA,ECvCnC,MDuCmC,CAAA,MAAA,EAAA,MAAA,CAAA;EAA4B,IAAA,EAAA,OAAA;EAAC,KAAA,ECrClE,MDqCkE,CAAA,MAAA,EAAA,MAAA,CAAA;EA+D3D,MAAA,EAAA,MAAA;EACL,QAAA,EAAA,MAAA;EACA,MAAA,CAAA,EAAA,MAAA;EACR,KAAA,CAAA,EAAA,MAAA;EAAM,QAAA,CAAA,EAAA,MAAA;;;;EC7GQ,OAAA,CAAA,EAAA,MAAW;EAIjB,SAAA,EAYE,IAZF;;;AAYM,KAIL,YAAA,GAJK,OAAA;AAIjB;AAGiB,UAAA,OAAA,CACN;EAKM,OAAA,EALN,WAKM;EAQA,QAAA,EAZL,YAYwB;AAMpC;AAuBA;AAMY,UA3CK,oBAAA,CA2CL;EAIC;EAEO,gBAAA,CAAA,EAAA,MAAA;EAIO;EAIR,YAAA,CAAA,EAAA,MAAA;;;AAII,UArDN,mBAAA,CAqDM;EAiIP;EA4DH,QAAA,CAAA,EAAA,MAAA;;;UA5OI,YAAA;;;;;;;;;;;UAuBA,oBAAA;;;;;;YAML;;;;aAIC;;oBAEO;;;;;;2BAIO;;;;mBAIR;;kBAED;;WAEP;;;;;;;;;;;;;;;;;;;;iBAiIK,aAAA,UAAuB,uBAAoB,OAAA,CAAA;;cA4D9C,4BAAmB"}
|
package/dist/index.mjs
CHANGED
|
@@ -126,6 +126,16 @@ function sanitizeHeaders(headers, config) {
|
|
|
126
126
|
* }))
|
|
127
127
|
* ```
|
|
128
128
|
*/
|
|
129
|
+
/** 默认排除的路径(健康检查等) */
|
|
130
|
+
const DEFAULT_EXCLUDE_PATHS = [
|
|
131
|
+
"/health",
|
|
132
|
+
"/healthz",
|
|
133
|
+
"/ready",
|
|
134
|
+
"/readiness",
|
|
135
|
+
"/liveness",
|
|
136
|
+
"/metrics",
|
|
137
|
+
"/favicon.ico"
|
|
138
|
+
];
|
|
129
139
|
var CircuitBreaker = class {
|
|
130
140
|
state = "closed";
|
|
131
141
|
failureCount = 0;
|
|
@@ -194,6 +204,24 @@ var ErrorThrottle = class {
|
|
|
194
204
|
}
|
|
195
205
|
};
|
|
196
206
|
/**
|
|
207
|
+
* 默认错误处理函数
|
|
208
|
+
* - 输出结构化 JSON 到 stdout(K8s 友好)
|
|
209
|
+
* - 使用 warn 级别(level: 40)
|
|
210
|
+
* - 包含 droppedCount 信息
|
|
211
|
+
*/
|
|
212
|
+
function defaultOnError(error, context) {
|
|
213
|
+
const { droppedCount } = context;
|
|
214
|
+
const log = {
|
|
215
|
+
level: 40,
|
|
216
|
+
time: Date.now(),
|
|
217
|
+
errorName: error.name,
|
|
218
|
+
errorMessage: error.message,
|
|
219
|
+
droppedCount,
|
|
220
|
+
msg: droppedCount > 0 ? `request-logger 上报失败 (已忽略 ${droppedCount} 条相同错误)` : "request-logger 上报失败"
|
|
221
|
+
};
|
|
222
|
+
console.log(JSON.stringify(log));
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
197
225
|
* 请求日志中间件
|
|
198
226
|
*
|
|
199
227
|
* @example
|
|
@@ -208,13 +236,15 @@ var ErrorThrottle = class {
|
|
|
208
236
|
* ```
|
|
209
237
|
*/
|
|
210
238
|
function requestLogger(options) {
|
|
211
|
-
const { url, service, headers = {}, timeout = 5e3, sanitize: sanitizeConfig, onError =
|
|
239
|
+
const { url, service, headers = {}, timeout = 5e3, sanitize: sanitizeConfig, onError = defaultOnError, enabled = true, excludePaths = [], useDefaultExcludePaths = true, circuitBreaker: circuitBreakerConfig, errorThrottle: errorThrottleConfig, stdout: stdoutConfig, sampleRate = 1, requestIdHeader = "x-request-id" } = options;
|
|
240
|
+
const allExcludePaths = useDefaultExcludePaths ? [...DEFAULT_EXCLUDE_PATHS, ...excludePaths] : excludePaths;
|
|
212
241
|
const circuitBreaker = new CircuitBreaker(circuitBreakerConfig);
|
|
213
242
|
const errorThrottle = new ErrorThrottle(errorThrottleConfig);
|
|
214
243
|
return defineMiddleware(async (req, next) => {
|
|
215
244
|
if (!enabled) return next();
|
|
216
245
|
const startTime = Date.now();
|
|
217
246
|
const response = await next();
|
|
247
|
+
if (sampleRate < 1 && Math.random() > sampleRate) return response;
|
|
218
248
|
recordLog(req, response, startTime, {
|
|
219
249
|
url,
|
|
220
250
|
service,
|
|
@@ -222,9 +252,11 @@ function requestLogger(options) {
|
|
|
222
252
|
timeout,
|
|
223
253
|
sanitizeConfig,
|
|
224
254
|
onError,
|
|
225
|
-
excludePaths,
|
|
255
|
+
excludePaths: allExcludePaths,
|
|
226
256
|
circuitBreaker,
|
|
227
|
-
errorThrottle
|
|
257
|
+
errorThrottle,
|
|
258
|
+
stdoutConfig,
|
|
259
|
+
requestIdHeader
|
|
228
260
|
}).catch(() => {});
|
|
229
261
|
return response;
|
|
230
262
|
});
|
|
@@ -259,13 +291,30 @@ async function fetchWithTimeout(targetUrl, options, timeout) {
|
|
|
259
291
|
clearTimeout(timeoutId);
|
|
260
292
|
}
|
|
261
293
|
}
|
|
294
|
+
/** 从请求中提取客户端 IP */
|
|
295
|
+
function getClientIp(req) {
|
|
296
|
+
const forwarded = req.headers.get("x-forwarded-for");
|
|
297
|
+
if (forwarded) return forwarded.split(",")[0].trim();
|
|
298
|
+
return req.headers.get("x-real-ip") ?? req.headers.get("cf-connecting-ip") ?? req.headers.get("true-client-ip") ?? void 0;
|
|
299
|
+
}
|
|
300
|
+
/** 从请求中提取 Request ID */
|
|
301
|
+
function getRequestId(req, headerName) {
|
|
302
|
+
const reqWithId = req;
|
|
303
|
+
if (reqWithId.id) return reqWithId.id;
|
|
304
|
+
return req.headers.get(headerName) ?? void 0;
|
|
305
|
+
}
|
|
306
|
+
/** 根据状态码获取日志级别 */
|
|
307
|
+
function getLogLevel(status) {
|
|
308
|
+
if (status >= 500) return 50;
|
|
309
|
+
if (status >= 400) return 40;
|
|
310
|
+
return 30;
|
|
311
|
+
}
|
|
262
312
|
async function recordLog(req, response, startTime, options) {
|
|
263
|
-
const { url: logUrl, service, headers: customHeaders, timeout, sanitizeConfig, onError, excludePaths, circuitBreaker, errorThrottle } = options;
|
|
313
|
+
const { url: logUrl, service, headers: customHeaders, timeout, sanitizeConfig, onError, excludePaths, circuitBreaker, errorThrottle, stdoutConfig, requestIdHeader } = options;
|
|
264
314
|
const reqUrl = new URL(req.url);
|
|
265
315
|
const path = reqUrl.pathname;
|
|
266
316
|
if (isPathExcluded(path, excludePaths)) return;
|
|
267
317
|
if (shouldSkipLog(req.method, path)) return;
|
|
268
|
-
if (!circuitBreaker.canRequest()) return;
|
|
269
318
|
let body = null;
|
|
270
319
|
try {
|
|
271
320
|
if ((req.headers.get("content-type") || "").includes("application/json")) body = await req.clone().json();
|
|
@@ -281,6 +330,9 @@ async function recordLog(req, response, startTime, options) {
|
|
|
281
330
|
const sanitizedHeaders = sanitizeHeaders(headers, sanitizeConfig);
|
|
282
331
|
const sanitizedBody = sanitize(body, sanitizeConfig);
|
|
283
332
|
const sanitizedResponseData = sanitize(responseData, sanitizeConfig);
|
|
333
|
+
const duration = Date.now() - startTime;
|
|
334
|
+
const clientIp = getClientIp(req);
|
|
335
|
+
const requestId = getRequestId(req, requestIdHeader);
|
|
284
336
|
const logBody = {
|
|
285
337
|
method: req.method,
|
|
286
338
|
url: req.url,
|
|
@@ -289,11 +341,15 @@ async function recordLog(req, response, startTime, options) {
|
|
|
289
341
|
body: sanitizedBody,
|
|
290
342
|
query: Object.fromEntries(reqUrl.searchParams),
|
|
291
343
|
status: response.status,
|
|
292
|
-
duration
|
|
344
|
+
duration,
|
|
293
345
|
service,
|
|
294
346
|
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
295
347
|
response: sanitizedResponseData
|
|
296
348
|
};
|
|
349
|
+
if (clientIp) logBody.clientIp = clientIp;
|
|
350
|
+
if (requestId) logBody.requestId = requestId;
|
|
351
|
+
if (stdoutConfig?.enabled !== false) writeToStdout(logBody, stdoutConfig ?? {});
|
|
352
|
+
if (!circuitBreaker.canRequest()) return;
|
|
297
353
|
try {
|
|
298
354
|
const res = await fetchWithTimeout(logUrl, {
|
|
299
355
|
method: "POST",
|
|
@@ -311,6 +367,30 @@ async function recordLog(req, response, startTime, options) {
|
|
|
311
367
|
if (shouldLog) onError(error, { droppedCount });
|
|
312
368
|
}
|
|
313
369
|
}
|
|
370
|
+
/** 输出到 stdout(用于 K8s 日志采集) */
|
|
371
|
+
function writeToStdout(logBody, config) {
|
|
372
|
+
const { format = "json", includeBody = true, includeResponse = false } = config;
|
|
373
|
+
const status = logBody.status;
|
|
374
|
+
const stdoutLog = {
|
|
375
|
+
level: getLogLevel(status),
|
|
376
|
+
time: Date.now(),
|
|
377
|
+
service: logBody.service,
|
|
378
|
+
method: logBody.method,
|
|
379
|
+
path: logBody.path,
|
|
380
|
+
status,
|
|
381
|
+
duration: logBody.duration,
|
|
382
|
+
msg: `${logBody.method} ${logBody.path} ${status} ${logBody.duration}ms`
|
|
383
|
+
};
|
|
384
|
+
if (logBody.requestId) stdoutLog.requestId = logBody.requestId;
|
|
385
|
+
if (logBody.clientIp) stdoutLog.clientIp = logBody.clientIp;
|
|
386
|
+
if (includeBody && logBody.body) stdoutLog.body = logBody.body;
|
|
387
|
+
if (includeResponse && logBody.response) stdoutLog.response = logBody.response;
|
|
388
|
+
if (format === "json") console.log(JSON.stringify(stdoutLog));
|
|
389
|
+
else {
|
|
390
|
+
const reqId = logBody.requestId ? ` [${logBody.requestId}]` : "";
|
|
391
|
+
console.log(`[${(/* @__PURE__ */ new Date()).toISOString()}]${reqId} ${logBody.method} ${logBody.path} ${status} ${logBody.duration}ms`);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
314
394
|
var src_default = requestLogger;
|
|
315
395
|
|
|
316
396
|
//#endregion
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/sanitize.ts","../src/index.ts"],"sourcesContent":["/**\n * 敏感数据清洗工具\n * \n * 用于在记录日志前移除或脱敏敏感信息\n */\n\n// ============ Types ============\n\nexport interface SanitizeConfig {\n /** 需要完全移除的字段(小写) */\n removeFields?: string[]\n /** 需要脱敏的字段(小写,部分匹配) */\n maskFields?: string[]\n /** 脱敏占位符 @default '[REDACTED]' */\n placeholder?: string\n /** 最大递归深度 @default 10 */\n maxDepth?: number\n}\n\n// ============ Default Config ============\n\n/** 默认需要完全移除的敏感字段 */\nconst DEFAULT_REMOVE_FIELDS = [\n 'password',\n 'newpassword',\n 'oldpassword',\n 'confirmpassword',\n 'secret',\n 'secretkey',\n 'privatekey',\n 'apisecret',\n 'clientsecret',\n]\n\n/** 默认需要脱敏的字段(保留部分信息) */\nconst DEFAULT_MASK_FIELDS = [\n 'token',\n 'accesstoken',\n 'refreshtoken',\n 'authorization',\n 'apikey',\n 'api_key',\n 'x-api-key',\n 'idtoken',\n 'sessiontoken',\n 'bearer',\n]\n\nconst DEFAULT_PLACEHOLDER = '[REDACTED]'\nconst DEFAULT_MAX_DEPTH = 10\n\n// ============ Sanitize Functions ============\n\n/**\n * 部分脱敏(保留前4后4位)\n */\nfunction partialMask(value: string, placeholder: string): string {\n if (value.length <= 8) return placeholder\n return value.slice(0, 4) + '****' + value.slice(-4)\n}\n\n/**\n * 深度清洗对象中的敏感数据\n * \n * @example\n * ```typescript\n * const data = { password: '123456', token: 'eyJhbG...' }\n * const sanitized = sanitize(data)\n * // { password: '[REDACTED]', token: 'eyJh****...' }\n * ```\n */\nexport function sanitize<T>(data: T, config?: SanitizeConfig, depth = 0): T {\n const {\n removeFields = DEFAULT_REMOVE_FIELDS,\n maskFields = DEFAULT_MASK_FIELDS,\n placeholder = DEFAULT_PLACEHOLDER,\n maxDepth = DEFAULT_MAX_DEPTH,\n } = config ?? {}\n\n // 防止无限递归\n if (depth > maxDepth) return data\n \n if (data === null || data === undefined) {\n return data\n }\n\n // 处理数组\n if (Array.isArray(data)) {\n return data.map(item => sanitize(item, config, depth + 1)) as T\n }\n\n // 处理对象\n if (typeof data === 'object') {\n const result: Record<string, unknown> = {}\n \n for (const [key, value] of Object.entries(data)) {\n const lowerKey = key.toLowerCase()\n \n // 完全移除的字段\n if (removeFields.some(field => lowerKey === field)) {\n result[key] = placeholder\n continue\n }\n \n // 部分脱敏的字段\n if (maskFields.some(field => lowerKey.includes(field))) {\n if (typeof value === 'string') {\n result[key] = partialMask(value, placeholder)\n } else {\n result[key] = placeholder\n }\n continue\n }\n \n // 递归处理嵌套对象\n result[key] = sanitize(value, config, depth + 1)\n }\n \n return result as T\n }\n\n return data\n}\n\n/**\n * 清洗 HTTP 请求头\n * \n * @example\n * ```typescript\n * const headers = { Authorization: 'Bearer eyJhbG...', Cookie: 'session=xxx' }\n * const sanitized = sanitizeHeaders(headers)\n * // { Authorization: 'Bearer eyJh****...', Cookie: '[REDACTED]' }\n * ```\n */\nexport function sanitizeHeaders(\n headers: Record<string, string>,\n config?: SanitizeConfig\n): Record<string, string> {\n const {\n maskFields = DEFAULT_MASK_FIELDS,\n placeholder = DEFAULT_PLACEHOLDER,\n } = config ?? {}\n\n const result: Record<string, string> = {}\n \n for (const [key, value] of Object.entries(headers)) {\n const lowerKey = key.toLowerCase()\n \n // Authorization 头部分脱敏\n if (lowerKey === 'authorization') {\n if (value.startsWith('Bearer ')) {\n result[key] = 'Bearer ' + partialMask(value.slice(7), placeholder)\n } else {\n result[key] = partialMask(value, placeholder)\n }\n continue\n }\n \n // Cookie 完全脱敏\n if (lowerKey === 'cookie' || lowerKey === 'set-cookie') {\n result[key] = placeholder\n continue\n }\n \n // API Key 相关头脱敏\n if (maskFields.some(field => lowerKey.includes(field))) {\n result[key] = partialMask(value, placeholder)\n continue\n }\n \n result[key] = value\n }\n \n return result\n}\n\n/**\n * 检查值是否为敏感字段\n */\nexport function isSensitiveField(fieldName: string, config?: SanitizeConfig): boolean {\n const {\n removeFields = DEFAULT_REMOVE_FIELDS,\n maskFields = DEFAULT_MASK_FIELDS,\n } = config ?? {}\n\n const lowerName = fieldName.toLowerCase()\n \n return (\n removeFields.some(field => lowerName === field) ||\n maskFields.some(field => lowerName.includes(field))\n )\n}\n\n","/**\n * @vafast/request-logger - API 请求日志中间件\n *\n * 特性:\n * - 自动敏感数据脱敏\n * - HTTP 远程日志服务\n * - 异步非阻塞记录\n * - 路由级别日志控制(路由定义中设置 log: false)\n * - 熔断器:连续失败后暂停上报,避免无谓等待\n * - 错误节流:同类错误在一段时间内只打一次日志\n *\n * @example\n * ```typescript\n * import { requestLogger } from '@vafast/request-logger'\n *\n * server.use(requestLogger({\n * url: 'http://log-server:9005/api/logs/ingest',\n * service: 'auth-server',\n * getUserId: (req) => req.__locals?.userInfo?.id,\n * }))\n * ```\n */\nimport { defineMiddleware, getRoute } from 'vafast'\nimport { sanitize, sanitizeHeaders, type SanitizeConfig } from './sanitize'\n\n// ============ Types ============\n\n/** 请求信息 */\nexport interface RequestData {\n method: string\n url: string\n path: string\n headers: Record<string, string>\n body: unknown\n query: Record<string, string>\n status: number\n duration: number\n userId?: string\n appId?: string\n authType?: string\n service?: string\n ip?: string\n userAgent?: string\n traceId?: string\n createdAt: Date\n}\n\n/** 响应数据(完整响应体) */\nexport type ResponseData = unknown\n\n/** 完整日志数据 */\nexport interface LogData {\n request: RequestData\n response: ResponseData\n}\n\n/** 熔断器配置 */\nexport interface CircuitBreakerConfig {\n /** 触发熔断的连续失败次数,默认 5 */\n failureThreshold?: number\n /** 熔断恢复时间(毫秒),默认 60000(1分钟) */\n resetTimeout?: number\n}\n\n/** 错误节流配置 */\nexport interface ErrorThrottleConfig {\n /** 同类错误的节流间隔(毫秒),默认 60000(1分钟) */\n interval?: number\n}\n\n/** 请求日志配置 */\nexport interface RequestLoggerOptions {\n /** 日志服务 URL */\n url: string\n /** 服务标识(如 auth-server、ones-server) */\n service: string\n /** 自定义请求头(如认证信息) */\n headers?: Record<string, string>\n /** 超时时间(毫秒),默认 5000 */\n timeout?: number\n /** 敏感数据清洗配置 */\n sanitize?: SanitizeConfig\n /** 错误回调 */\n onError?: (error: Error, context: { droppedCount: number }) => void\n /** 是否启用 @default true */\n enabled?: boolean\n /** 排除的路径列表(精确匹配或正则),这些路径不记录日志 */\n excludePaths?: (string | RegExp)[]\n /** 熔断器配置 */\n circuitBreaker?: CircuitBreakerConfig\n /** 错误节流配置 */\n errorThrottle?: ErrorThrottleConfig\n}\n\n// ============ Circuit Breaker ============\n\ntype CircuitState = 'closed' | 'open' | 'half-open'\n\nclass CircuitBreaker {\n private state: CircuitState = 'closed'\n private failureCount = 0\n private lastFailureTime = 0\n private readonly failureThreshold: number\n private readonly resetTimeout: number\n\n constructor(config: CircuitBreakerConfig = {}) {\n this.failureThreshold = config.failureThreshold ?? 5\n this.resetTimeout = config.resetTimeout ?? 60000\n }\n\n /** 检查是否允许请求 */\n canRequest(): boolean {\n if (this.state === 'closed') return true\n\n if (this.state === 'open') {\n // 检查是否到了恢复时间\n if (Date.now() - this.lastFailureTime >= this.resetTimeout) {\n this.state = 'half-open'\n return true\n }\n return false\n }\n\n // half-open 状态允许一个请求通过测试\n return true\n }\n\n /** 记录成功 */\n recordSuccess(): void {\n this.failureCount = 0\n this.state = 'closed'\n }\n\n /** 记录失败 */\n recordFailure(): void {\n this.failureCount++\n this.lastFailureTime = Date.now()\n\n if (this.failureCount >= this.failureThreshold) {\n this.state = 'open'\n }\n }\n\n /** 获取当前状态信息 */\n getStatus(): { state: CircuitState; failureCount: number } {\n return { state: this.state, failureCount: this.failureCount }\n }\n}\n\n// ============ Error Throttle ============\n\nclass ErrorThrottle {\n private lastErrorTime = 0\n private droppedCount = 0\n private readonly interval: number\n\n constructor(config: ErrorThrottleConfig = {}) {\n this.interval = config.interval ?? 60000\n }\n\n /** 检查是否应该打印错误,返回 { shouldLog, droppedCount } */\n shouldLog(): { shouldLog: boolean; droppedCount: number } {\n const now = Date.now()\n\n if (now - this.lastErrorTime >= this.interval) {\n const dropped = this.droppedCount\n this.lastErrorTime = now\n this.droppedCount = 0\n return { shouldLog: true, droppedCount: dropped }\n }\n\n this.droppedCount++\n return { shouldLog: false, droppedCount: 0 }\n }\n}\n\n// ============ Middleware ============\n\n/**\n * 请求日志中间件\n *\n * @example\n * ```typescript\n * import { requestLogger } from '@vafast/request-logger'\n *\n * server.use(requestLogger({\n * url: 'http://log-server:9005/api/logs/ingest',\n * service: 'auth-server',\n * auth: { apiKeyId: 'xxx', apiKeySecret: 'yyy' },\n * }))\n * ```\n */\nexport function requestLogger(options: RequestLoggerOptions) {\n const {\n url,\n service,\n headers = {},\n timeout = 5000,\n sanitize: sanitizeConfig,\n onError = console.error,\n enabled = true,\n excludePaths = [],\n circuitBreaker: circuitBreakerConfig,\n errorThrottle: errorThrottleConfig,\n } = options\n\n // 创建熔断器和错误节流器实例\n const circuitBreaker = new CircuitBreaker(circuitBreakerConfig)\n const errorThrottle = new ErrorThrottle(errorThrottleConfig)\n\n return defineMiddleware(async (req, next) => {\n if (!enabled) return next()\n\n const startTime = Date.now()\n const response = await next()\n\n // 异步记录日志,不阻塞响应\n recordLog(req, response, startTime, {\n url,\n service,\n headers,\n timeout,\n sanitizeConfig,\n onError,\n excludePaths,\n circuitBreaker,\n errorThrottle,\n }).catch(() => {\n // 错误已在 recordLog 内部处理,这里静默忽略\n })\n\n return response\n })\n}\n\n/** @deprecated 使用 requestLogger 代替 */\nexport const createRequestLogger = requestLogger\n\n// ============ Internal ============\n\ninterface RecordLogOptions {\n url: string\n service: string\n headers: Record<string, string>\n timeout: number\n sanitizeConfig?: SanitizeConfig\n onError: (error: Error, context: { droppedCount: number }) => void\n excludePaths: (string | RegExp)[]\n circuitBreaker: CircuitBreaker\n errorThrottle: ErrorThrottle\n}\n\n/** 检查路由是否配置了 log: false */\nfunction shouldSkipLog(method: string, path: string): boolean {\n try {\n const route = getRoute<{ log?: boolean }>(method, path)\n return route?.log === false\n } catch {\n return false\n }\n}\n\n/** 检查路径是否在排除列表中 */\nfunction isPathExcluded(\n path: string,\n excludePaths: (string | RegExp)[]\n): boolean {\n return excludePaths.some((pattern) => {\n if (typeof pattern === 'string') {\n return path === pattern || path.startsWith(pattern + '/')\n }\n return pattern.test(path)\n })\n}\n\n/** 带超时的 fetch */\nasync function fetchWithTimeout(\n targetUrl: string,\n options: RequestInit,\n timeout: number\n): Promise<Response> {\n const controller = new AbortController()\n const timeoutId = setTimeout(() => controller.abort(), timeout)\n\n try {\n return await fetch(targetUrl, { ...options, signal: controller.signal })\n } finally {\n clearTimeout(timeoutId)\n }\n}\n\nasync function recordLog(\n req: Request,\n response: Response,\n startTime: number,\n options: RecordLogOptions\n) {\n const {\n url: logUrl,\n service,\n headers: customHeaders,\n timeout,\n sanitizeConfig,\n onError,\n excludePaths,\n circuitBreaker,\n errorThrottle,\n } = options\n\n const reqUrl = new URL(req.url)\n const path = reqUrl.pathname\n\n // 检查路径是否在排除列表中\n if (isPathExcluded(path, excludePaths)) return\n\n // 检查路由是否禁用日志\n if (shouldSkipLog(req.method, path)) return\n\n // 熔断器检查:如果熔断打开,直接跳过上报\n if (!circuitBreaker.canRequest()) {\n return\n }\n\n // 解析请求体\n let body: unknown = null\n try {\n const contentType = req.headers.get('content-type') || ''\n if (contentType.includes('application/json')) {\n body = await req.clone().json()\n }\n } catch {\n // 忽略\n }\n\n // 解析响应体\n let responseData: ResponseData = null\n try {\n responseData = await response.clone().json()\n } catch {\n // 忽略(非 JSON 响应)\n }\n\n // 提取请求头\n const headers: Record<string, string> = {}\n req.headers.forEach((value, key) => {\n headers[key] = value\n })\n\n // 清洗敏感数据\n const sanitizedHeaders = sanitizeHeaders(headers, sanitizeConfig)\n const sanitizedBody = sanitize(body, sanitizeConfig)\n const sanitizedResponseData = sanitize(responseData, sanitizeConfig)\n\n // 构建日志数据(业务字段由 log-server 从 headers 解析)\n const logBody = {\n method: req.method,\n url: req.url,\n path,\n headers: sanitizedHeaders,\n body: sanitizedBody,\n query: Object.fromEntries(reqUrl.searchParams),\n status: response.status,\n duration: Date.now() - startTime,\n service,\n createdAt: new Date().toISOString(),\n response: sanitizedResponseData, // 直接存储完整响应数据\n }\n\n // 发送到日志服务\n try {\n const res = await fetchWithTimeout(\n logUrl,\n {\n method: 'POST',\n headers: { 'Content-Type': 'application/json', ...customHeaders },\n body: JSON.stringify(logBody),\n },\n timeout\n )\n\n if (!res.ok) {\n throw new Error(`HTTP ${res.status}: ${res.statusText}`)\n }\n\n // 成功:重置熔断器\n circuitBreaker.recordSuccess()\n } catch (error) {\n // 失败:记录到熔断器\n circuitBreaker.recordFailure()\n\n // 错误节流:检查是否应该打印\n const { shouldLog, droppedCount } = errorThrottle.shouldLog()\n if (shouldLog) {\n onError(error as Error, { droppedCount })\n }\n }\n}\n\n// ============ Re-exports ============\n\nexport { sanitize, sanitizeHeaders, type SanitizeConfig } from './sanitize'\nexport default requestLogger\n"],"mappings":";;;;AAsBA,MAAM,wBAAwB;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;;AAGD,MAAM,sBAAsB;CAC1B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;AAED,MAAM,sBAAsB;AAC5B,MAAM,oBAAoB;;;;AAO1B,SAAS,YAAY,OAAe,aAA6B;AAC/D,KAAI,MAAM,UAAU,EAAG,QAAO;AAC9B,QAAO,MAAM,MAAM,GAAG,EAAE,GAAG,SAAS,MAAM,MAAM,GAAG;;;;;;;;;;;;AAarD,SAAgB,SAAY,MAAS,QAAyB,QAAQ,GAAM;CAC1E,MAAM,EACJ,eAAe,uBACf,aAAa,qBACb,cAAc,qBACd,WAAW,sBACT,UAAU,EAAE;AAGhB,KAAI,QAAQ,SAAU,QAAO;AAE7B,KAAI,SAAS,QAAQ,SAAS,OAC5B,QAAO;AAIT,KAAI,MAAM,QAAQ,KAAK,CACrB,QAAO,KAAK,KAAI,SAAQ,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I,eAA6B;AACjC,KAAI;AACF,iBAAe,MAAM,SAAS,OAAO,CAAC,MAAM;SACtC;CAKR,MAAM,UAAkC,EAAE;AAC1C,KAAI,QAAQ,SAAS,OAAO,QAAQ;AAClC,UAAQ,OAAO;GACf;CAGF,MAAM,mBAAmB,gBAAgB,SAAS,eAAe;CACjE,MAAM,gBAAgB,SAAS,MAAM,eAAe;CACpD,MAAM,wBAAwB,SAAS,cAAc,eAAe;CAGpE,MAAM,UAAU;EACd,QAAQ,IAAI;EACZ,KAAK,IAAI;EACT;EACA,SAAS;EACT,MAAM;EACN,OAAO,OAAO,YAAY,OAAO,aAAa;EAC9C,QAAQ,SAAS;EACjB,UAAU,KAAK,KAAK,GAAG;EACvB;EACA,4BAAW,IAAI,MAAM,EAAC,aAAa;EACnC,UAAU;EACX;AAGD,KAAI;EACF,MAAM,MAAM,MAAM,iBAChB,QACA;GACE,QAAQ;GACR,SAAS;IAAE,gBAAgB;IAAoB,GAAG;IAAe;GACjE,MAAM,KAAK,UAAU,QAAQ;GAC9B,EACD,QACD;AAED,MAAI,CAAC,IAAI,GACP,OAAM,IAAI,MAAM,QAAQ,IAAI,OAAO,IAAI,IAAI,aAAa;AAI1D,iBAAe,eAAe;UACvB,OAAO;AAEd,iBAAe,eAAe;EAG9B,MAAM,EAAE,WAAW,iBAAiB,cAAc,WAAW;AAC7D,MAAI,UACF,SAAQ,OAAgB,EAAE,cAAc,CAAC;;;AAQ/C,kBAAe"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/sanitize.ts","../src/index.ts"],"sourcesContent":["/**\n * 敏感数据清洗工具\n * \n * 用于在记录日志前移除或脱敏敏感信息\n */\n\n// ============ Types ============\n\nexport interface SanitizeConfig {\n /** 需要完全移除的字段(小写) */\n removeFields?: string[]\n /** 需要脱敏的字段(小写,部分匹配) */\n maskFields?: string[]\n /** 脱敏占位符 @default '[REDACTED]' */\n placeholder?: string\n /** 最大递归深度 @default 10 */\n maxDepth?: number\n}\n\n// ============ Default Config ============\n\n/** 默认需要完全移除的敏感字段 */\nconst DEFAULT_REMOVE_FIELDS = [\n 'password',\n 'newpassword',\n 'oldpassword',\n 'confirmpassword',\n 'secret',\n 'secretkey',\n 'privatekey',\n 'apisecret',\n 'clientsecret',\n]\n\n/** 默认需要脱敏的字段(保留部分信息) */\nconst DEFAULT_MASK_FIELDS = [\n 'token',\n 'accesstoken',\n 'refreshtoken',\n 'authorization',\n 'apikey',\n 'api_key',\n 'x-api-key',\n 'idtoken',\n 'sessiontoken',\n 'bearer',\n]\n\nconst DEFAULT_PLACEHOLDER = '[REDACTED]'\nconst DEFAULT_MAX_DEPTH = 10\n\n// ============ Sanitize Functions ============\n\n/**\n * 部分脱敏(保留前4后4位)\n */\nfunction partialMask(value: string, placeholder: string): string {\n if (value.length <= 8) return placeholder\n return value.slice(0, 4) + '****' + value.slice(-4)\n}\n\n/**\n * 深度清洗对象中的敏感数据\n * \n * @example\n * ```typescript\n * const data = { password: '123456', token: 'eyJhbG...' }\n * const sanitized = sanitize(data)\n * // { password: '[REDACTED]', token: 'eyJh****...' }\n * ```\n */\nexport function sanitize<T>(data: T, config?: SanitizeConfig, depth = 0): T {\n const {\n removeFields = DEFAULT_REMOVE_FIELDS,\n maskFields = DEFAULT_MASK_FIELDS,\n placeholder = DEFAULT_PLACEHOLDER,\n maxDepth = DEFAULT_MAX_DEPTH,\n } = config ?? {}\n\n // 防止无限递归\n if (depth > maxDepth) return data\n \n if (data === null || data === undefined) {\n return data\n }\n\n // 处理数组\n if (Array.isArray(data)) {\n return data.map(item => sanitize(item, config, depth + 1)) as T\n }\n\n // 处理对象\n if (typeof data === 'object') {\n const result: Record<string, unknown> = {}\n \n for (const [key, value] of Object.entries(data)) {\n const lowerKey = key.toLowerCase()\n \n // 完全移除的字段\n if (removeFields.some(field => lowerKey === field)) {\n result[key] = placeholder\n continue\n }\n \n // 部分脱敏的字段\n if (maskFields.some(field => lowerKey.includes(field))) {\n if (typeof value === 'string') {\n result[key] = partialMask(value, placeholder)\n } else {\n result[key] = placeholder\n }\n continue\n }\n \n // 递归处理嵌套对象\n result[key] = sanitize(value, config, depth + 1)\n }\n \n return result as T\n }\n\n return data\n}\n\n/**\n * 清洗 HTTP 请求头\n * \n * @example\n * ```typescript\n * const headers = { Authorization: 'Bearer eyJhbG...', Cookie: 'session=xxx' }\n * const sanitized = sanitizeHeaders(headers)\n * // { Authorization: 'Bearer eyJh****...', Cookie: '[REDACTED]' }\n * ```\n */\nexport function sanitizeHeaders(\n headers: Record<string, string>,\n config?: SanitizeConfig\n): Record<string, string> {\n const {\n maskFields = DEFAULT_MASK_FIELDS,\n placeholder = DEFAULT_PLACEHOLDER,\n } = config ?? {}\n\n const result: Record<string, string> = {}\n \n for (const [key, value] of Object.entries(headers)) {\n const lowerKey = key.toLowerCase()\n \n // Authorization 头部分脱敏\n if (lowerKey === 'authorization') {\n if (value.startsWith('Bearer ')) {\n result[key] = 'Bearer ' + partialMask(value.slice(7), placeholder)\n } else {\n result[key] = partialMask(value, placeholder)\n }\n continue\n }\n \n // Cookie 完全脱敏\n if (lowerKey === 'cookie' || lowerKey === 'set-cookie') {\n result[key] = placeholder\n continue\n }\n \n // API Key 相关头脱敏\n if (maskFields.some(field => lowerKey.includes(field))) {\n result[key] = partialMask(value, placeholder)\n continue\n }\n \n result[key] = value\n }\n \n return result\n}\n\n/**\n * 检查值是否为敏感字段\n */\nexport function isSensitiveField(fieldName: string, config?: SanitizeConfig): boolean {\n const {\n removeFields = DEFAULT_REMOVE_FIELDS,\n maskFields = DEFAULT_MASK_FIELDS,\n } = config ?? {}\n\n const lowerName = fieldName.toLowerCase()\n \n return (\n removeFields.some(field => lowerName === field) ||\n maskFields.some(field => lowerName.includes(field))\n )\n}\n\n","/**\n * @vafast/request-logger - API 请求日志中间件\n *\n * 特性:\n * - 自动敏感数据脱敏\n * - HTTP 远程日志服务\n * - 异步非阻塞记录\n * - 路由级别日志控制(路由定义中设置 log: false)\n * - 熔断器:连续失败后暂停上报,避免无谓等待\n * - 错误节流:同类错误在一段时间内只打一次日志\n *\n * @example\n * ```typescript\n * import { requestLogger } from '@vafast/request-logger'\n *\n * server.use(requestLogger({\n * url: 'http://log-server:9005/api/logs/ingest',\n * service: 'auth-server',\n * getUserId: (req) => req.__locals?.userInfo?.id,\n * }))\n * ```\n */\nimport { defineMiddleware, getRoute } from 'vafast'\nimport { sanitize, sanitizeHeaders, type SanitizeConfig } from './sanitize'\n\n// ============ Types ============\n\n/** 请求信息 */\nexport interface RequestData {\n method: string\n url: string\n path: string\n headers: Record<string, string>\n body: unknown\n query: Record<string, string>\n status: number\n duration: number\n userId?: string\n appId?: string\n authType?: string\n service?: string\n ip?: string\n userAgent?: string\n traceId?: string\n createdAt: Date\n}\n\n/** 响应数据(完整响应体) */\nexport type ResponseData = unknown\n\n/** 完整日志数据 */\nexport interface LogData {\n request: RequestData\n response: ResponseData\n}\n\n/** 熔断器配置 */\nexport interface CircuitBreakerConfig {\n /** 触发熔断的连续失败次数,默认 5 */\n failureThreshold?: number\n /** 熔断恢复时间(毫秒),默认 60000(1分钟) */\n resetTimeout?: number\n}\n\n/** 错误节流配置 */\nexport interface ErrorThrottleConfig {\n /** 同类错误的节流间隔(毫秒),默认 60000(1分钟) */\n interval?: number\n}\n\n/** stdout 双写配置 */\nexport interface StdoutConfig {\n /** 是否启用 stdout 输出 @default true */\n enabled?: boolean\n /** 输出格式 @default 'json' */\n format?: 'json' | 'text'\n /** 是否包含请求体 @default true */\n includeBody?: boolean\n /** 是否包含响应体(可能很大)@default false */\n includeResponse?: boolean\n}\n\n/** 默认排除的路径(健康检查等) */\nconst DEFAULT_EXCLUDE_PATHS = [\n '/health',\n '/healthz',\n '/ready',\n '/readiness',\n '/liveness',\n '/metrics',\n '/favicon.ico',\n]\n\n/** 请求日志配置 */\nexport interface RequestLoggerOptions {\n /** 日志服务 URL */\n url: string\n /** 服务标识(如 auth-server、ones-server) */\n service: string\n /** 自定义请求头(如认证信息) */\n headers?: Record<string, string>\n /** 超时时间(毫秒),默认 5000 */\n timeout?: number\n /** 敏感数据清洗配置 */\n sanitize?: SanitizeConfig\n /** 错误回调 */\n onError?: (error: Error, context: { droppedCount: number }) => void\n /** 是否启用 @default true */\n enabled?: boolean\n /** 排除的路径列表(精确匹配或正则),这些路径不记录日志 */\n excludePaths?: (string | RegExp)[]\n /** 是否使用默认排除路径(/health, /metrics 等)@default true */\n useDefaultExcludePaths?: boolean\n /** 熔断器配置 */\n circuitBreaker?: CircuitBreakerConfig\n /** 错误节流配置 */\n errorThrottle?: ErrorThrottleConfig\n /** stdout 双写配置(用于 K8s 日志采集) */\n stdout?: StdoutConfig\n /** 日志采样率 (0-1),1 表示记录所有请求,0.1 表示只记录 10% @default 1 */\n sampleRate?: number\n /** 请求 ID 的 header 名称,用于分布式追踪 @default 'x-request-id' */\n requestIdHeader?: string\n}\n\n// ============ Circuit Breaker ============\n\ntype CircuitState = 'closed' | 'open' | 'half-open'\n\nclass CircuitBreaker {\n private state: CircuitState = 'closed'\n private failureCount = 0\n private lastFailureTime = 0\n private readonly failureThreshold: number\n private readonly resetTimeout: number\n\n constructor(config: CircuitBreakerConfig = {}) {\n this.failureThreshold = config.failureThreshold ?? 5\n this.resetTimeout = config.resetTimeout ?? 60000\n }\n\n /** 检查是否允许请求 */\n canRequest(): boolean {\n if (this.state === 'closed') return true\n\n if (this.state === 'open') {\n // 检查是否到了恢复时间\n if (Date.now() - this.lastFailureTime >= this.resetTimeout) {\n this.state = 'half-open'\n return true\n }\n return false\n }\n\n // half-open 状态允许一个请求通过测试\n return true\n }\n\n /** 记录成功 */\n recordSuccess(): void {\n this.failureCount = 0\n this.state = 'closed'\n }\n\n /** 记录失败 */\n recordFailure(): void {\n this.failureCount++\n this.lastFailureTime = Date.now()\n\n if (this.failureCount >= this.failureThreshold) {\n this.state = 'open'\n }\n }\n\n /** 获取当前状态信息 */\n getStatus(): { state: CircuitState; failureCount: number } {\n return { state: this.state, failureCount: this.failureCount }\n }\n}\n\n// ============ Error Throttle ============\n\nclass ErrorThrottle {\n private lastErrorTime = 0\n private droppedCount = 0\n private readonly interval: number\n\n constructor(config: ErrorThrottleConfig = {}) {\n this.interval = config.interval ?? 60000\n }\n\n /** 检查是否应该打印错误,返回 { shouldLog, droppedCount } */\n shouldLog(): { shouldLog: boolean; droppedCount: number } {\n const now = Date.now()\n\n if (now - this.lastErrorTime >= this.interval) {\n const dropped = this.droppedCount\n this.lastErrorTime = now\n this.droppedCount = 0\n return { shouldLog: true, droppedCount: dropped }\n }\n\n this.droppedCount++\n return { shouldLog: false, droppedCount: 0 }\n }\n}\n\n// ============ Default Error Handler ============\n\n/**\n * 默认错误处理函数\n * - 输出结构化 JSON 到 stdout(K8s 友好)\n * - 使用 warn 级别(level: 40)\n * - 包含 droppedCount 信息\n */\nfunction defaultOnError(error: Error, context: { droppedCount: number }): void {\n const { droppedCount } = context\n const log = {\n level: 40, // warn\n time: Date.now(),\n errorName: error.name,\n errorMessage: error.message,\n droppedCount,\n msg:\n droppedCount > 0\n ? `request-logger 上报失败 (已忽略 ${droppedCount} 条相同错误)`\n : 'request-logger 上报失败',\n }\n console.log(JSON.stringify(log))\n}\n\n// ============ Middleware ============\n\n/**\n * 请求日志中间件\n *\n * @example\n * ```typescript\n * import { requestLogger } from '@vafast/request-logger'\n *\n * server.use(requestLogger({\n * url: 'http://log-server:9005/api/logs/ingest',\n * service: 'auth-server',\n * auth: { apiKeyId: 'xxx', apiKeySecret: 'yyy' },\n * }))\n * ```\n */\nexport function requestLogger(options: RequestLoggerOptions) {\n const {\n url,\n service,\n headers = {},\n timeout = 5000,\n sanitize: sanitizeConfig,\n onError = defaultOnError,\n enabled = true,\n excludePaths = [],\n useDefaultExcludePaths = true,\n circuitBreaker: circuitBreakerConfig,\n errorThrottle: errorThrottleConfig,\n stdout: stdoutConfig,\n sampleRate = 1,\n requestIdHeader = 'x-request-id',\n } = options\n\n // 合并默认排除路径\n const allExcludePaths = useDefaultExcludePaths\n ? [...DEFAULT_EXCLUDE_PATHS, ...excludePaths]\n : excludePaths\n\n // 创建熔断器和错误节流器实例\n const circuitBreaker = new CircuitBreaker(circuitBreakerConfig)\n const errorThrottle = new ErrorThrottle(errorThrottleConfig)\n\n return defineMiddleware(async (req, next) => {\n if (!enabled) return next()\n\n const startTime = Date.now()\n const response = await next()\n\n // 日志采样:随机跳过部分请求\n if (sampleRate < 1 && Math.random() > sampleRate) {\n return response\n }\n\n // 异步记录日志,不阻塞响应\n recordLog(req, response, startTime, {\n url,\n service,\n headers,\n timeout,\n sanitizeConfig,\n onError,\n excludePaths: allExcludePaths,\n circuitBreaker,\n errorThrottle,\n stdoutConfig,\n requestIdHeader,\n }).catch(() => {\n // 错误已在 recordLog 内部处理,这里静默忽略\n })\n\n return response\n })\n}\n\n/** @deprecated 使用 requestLogger 代替 */\nexport const createRequestLogger = requestLogger\n\n// ============ Internal ============\n\ninterface RecordLogOptions {\n url: string\n service: string\n headers: Record<string, string>\n timeout: number\n sanitizeConfig?: SanitizeConfig\n onError: (error: Error, context: { droppedCount: number }) => void\n excludePaths: (string | RegExp)[]\n circuitBreaker: CircuitBreaker\n errorThrottle: ErrorThrottle\n stdoutConfig?: StdoutConfig\n requestIdHeader: string\n}\n\n/** 检查路由是否配置了 log: false */\nfunction shouldSkipLog(method: string, path: string): boolean {\n try {\n const route = getRoute<{ log?: boolean }>(method, path)\n return route?.log === false\n } catch {\n return false\n }\n}\n\n/** 检查路径是否在排除列表中 */\nfunction isPathExcluded(\n path: string,\n excludePaths: (string | RegExp)[]\n): boolean {\n return excludePaths.some((pattern) => {\n if (typeof pattern === 'string') {\n return path === pattern || path.startsWith(pattern + '/')\n }\n return pattern.test(path)\n })\n}\n\n/** 带超时的 fetch */\nasync function fetchWithTimeout(\n targetUrl: string,\n options: RequestInit,\n timeout: number\n): Promise<Response> {\n const controller = new AbortController()\n const timeoutId = setTimeout(() => controller.abort(), timeout)\n\n try {\n return await fetch(targetUrl, { ...options, signal: controller.signal })\n } finally {\n clearTimeout(timeoutId)\n }\n}\n\n/** 从请求中提取客户端 IP */\nfunction getClientIp(req: Request): string | undefined {\n // 按优先级尝试获取真实 IP\n const forwarded = req.headers.get('x-forwarded-for')\n if (forwarded) {\n // X-Forwarded-For 可能包含多个 IP,第一个是客户端真实 IP\n return forwarded.split(',')[0].trim()\n }\n return (\n req.headers.get('x-real-ip') ??\n req.headers.get('cf-connecting-ip') ?? // Cloudflare\n req.headers.get('true-client-ip') ?? // Akamai\n undefined\n )\n}\n\n/** 从请求中提取 Request ID */\nfunction getRequestId(req: Request, headerName: string): string | undefined {\n // 优先从 req.id 获取(如果使用了 @vafast/request-id 中间件)\n const reqWithId = req as Request & { id?: string }\n if (reqWithId.id) {\n return reqWithId.id\n }\n // 否则从 header 获取\n return req.headers.get(headerName) ?? undefined\n}\n\n/** 根据状态码获取日志级别 */\nfunction getLogLevel(status: number): number {\n if (status >= 500) return 50 // ERROR\n if (status >= 400) return 40 // WARN\n return 30 // INFO\n}\n\nasync function recordLog(\n req: Request,\n response: Response,\n startTime: number,\n options: RecordLogOptions\n) {\n const {\n url: logUrl,\n service,\n headers: customHeaders,\n timeout,\n sanitizeConfig,\n onError,\n excludePaths,\n circuitBreaker,\n errorThrottle,\n stdoutConfig,\n requestIdHeader,\n } = options\n\n const reqUrl = new URL(req.url)\n const path = reqUrl.pathname\n\n // 检查路径是否在排除列表中\n if (isPathExcluded(path, excludePaths)) return\n\n // 检查路由是否禁用日志\n if (shouldSkipLog(req.method, path)) return\n\n // 解析请求体\n let body: unknown = null\n try {\n const contentType = req.headers.get('content-type') || ''\n if (contentType.includes('application/json')) {\n body = await req.clone().json()\n }\n } catch {\n // 忽略\n }\n\n // 解析响应体\n let responseData: ResponseData = null\n try {\n responseData = await response.clone().json()\n } catch {\n // 忽略(非 JSON 响应)\n }\n\n // 提取请求头\n const headers: Record<string, string> = {}\n req.headers.forEach((value, key) => {\n headers[key] = value\n })\n\n // 清洗敏感数据\n const sanitizedHeaders = sanitizeHeaders(headers, sanitizeConfig)\n const sanitizedBody = sanitize(body, sanitizeConfig)\n const sanitizedResponseData = sanitize(responseData, sanitizeConfig)\n\n const duration = Date.now() - startTime\n\n // 提取客户端 IP 和 Request ID\n const clientIp = getClientIp(req)\n const requestId = getRequestId(req, requestIdHeader)\n\n // 构建日志数据(业务字段由 log-server 从 headers 解析)\n const logBody: Record<string, unknown> = {\n method: req.method,\n url: req.url,\n path,\n headers: sanitizedHeaders,\n body: sanitizedBody,\n query: Object.fromEntries(reqUrl.searchParams),\n status: response.status,\n duration,\n service,\n createdAt: new Date().toISOString(),\n response: sanitizedResponseData, // 直接存储完整响应数据\n }\n\n // 可选字段(只在有值时添加)\n if (clientIp) logBody.clientIp = clientIp\n if (requestId) logBody.requestId = requestId\n\n // 双写:输出到 stdout(用于 K8s 日志采集,默认开启)\n if (stdoutConfig?.enabled !== false) {\n writeToStdout(logBody, stdoutConfig ?? {})\n }\n\n // 熔断器检查:如果熔断打开,直接跳过 HTTP 上报\n if (!circuitBreaker.canRequest()) {\n return\n }\n\n // 发送到日志服务\n try {\n const res = await fetchWithTimeout(\n logUrl,\n {\n method: 'POST',\n headers: { 'Content-Type': 'application/json', ...customHeaders },\n body: JSON.stringify(logBody),\n },\n timeout\n )\n\n if (!res.ok) {\n throw new Error(`HTTP ${res.status}: ${res.statusText}`)\n }\n\n // 成功:重置熔断器\n circuitBreaker.recordSuccess()\n } catch (error) {\n // 失败:记录到熔断器\n circuitBreaker.recordFailure()\n\n // 错误节流:检查是否应该打印\n const { shouldLog, droppedCount } = errorThrottle.shouldLog()\n if (shouldLog) {\n onError(error as Error, { droppedCount })\n }\n }\n}\n\n/** 输出到 stdout(用于 K8s 日志采集) */\nfunction writeToStdout(\n logBody: Record<string, unknown>,\n config: StdoutConfig\n): void {\n const { format = 'json', includeBody = true, includeResponse = false } =\n config\n\n const status = logBody.status as number\n\n // 构建精简版日志(避免 stdout 日志过大)\n const stdoutLog: Record<string, unknown> = {\n level: getLogLevel(status), // 根据状态码设置日志级别\n time: Date.now(),\n service: logBody.service,\n method: logBody.method,\n path: logBody.path,\n status,\n duration: logBody.duration,\n msg: `${logBody.method} ${logBody.path} ${status} ${logBody.duration}ms`,\n }\n\n // 可选字段\n if (logBody.requestId) stdoutLog.requestId = logBody.requestId\n if (logBody.clientIp) stdoutLog.clientIp = logBody.clientIp\n\n // 可选:包含请求体\n if (includeBody && logBody.body) {\n stdoutLog.body = logBody.body\n }\n\n // 可选:包含响应体\n if (includeResponse && logBody.response) {\n stdoutLog.response = logBody.response\n }\n\n if (format === 'json') {\n console.log(JSON.stringify(stdoutLog))\n } else {\n // text 格式:更易读\n const reqId = logBody.requestId ? ` [${logBody.requestId}]` : ''\n console.log(\n `[${new Date().toISOString()}]${reqId} ${logBody.method} ${logBody.path} ${status} ${logBody.duration}ms`\n )\n }\n}\n\n// ============ Re-exports ============\n\nexport { sanitize, sanitizeHeaders, type SanitizeConfig } from './sanitize'\nexport default requestLogger\n"],"mappings":";;;;AAsBA,MAAM,wBAAwB;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;;AAGD,MAAM,sBAAsB;CAC1B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;AAED,MAAM,sBAAsB;AAC5B,MAAM,oBAAoB;;;;AAO1B,SAAS,YAAY,OAAe,aAA6B;AAC/D,KAAI,MAAM,UAAU,EAAG,QAAO;AAC9B,QAAO,MAAM,MAAM,GAAG,EAAE,GAAG,SAAS,MAAM,MAAM,GAAG;;;;;;;;;;;;AAarD,SAAgB,SAAY,MAAS,QAAyB,QAAQ,GAAM;CAC1E,MAAM,EACJ,eAAe,uBACf,aAAa,qBACb,cAAc,qBACd,WAAW,sBACT,UAAU,EAAE;AAGhB,KAAI,QAAQ,SAAU,QAAO;AAE7B,KAAI,SAAS,QAAQ,SAAS,OAC5B,QAAO;AAIT,KAAI,MAAM,QAAQ,KAAK,CACrB,QAAO,KAAK,KAAI,SAAQ,SAAS,MAAM,QAAQ,QAA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Q;AACrD,KAAI,QAAQ,SAAU,WAAU,WAAW,QAAQ;AAGnD,KAAI,eAAe,QAAQ,KACzB,WAAU,OAAO,QAAQ;AAI3B,KAAI,mBAAmB,QAAQ,SAC7B,WAAU,WAAW,QAAQ;AAG/B,KAAI,WAAW,OACb,SAAQ,IAAI,KAAK,UAAU,UAAU,CAAC;MACjC;EAEL,MAAM,QAAQ,QAAQ,YAAY,KAAK,QAAQ,UAAU,KAAK;AAC9D,UAAQ,IACN,qBAAI,IAAI,MAAM,EAAC,aAAa,CAAC,GAAG,MAAM,GAAG,QAAQ,OAAO,GAAG,QAAQ,KAAK,GAAG,OAAO,GAAG,QAAQ,SAAS,IACvG;;;AAOL,kBAAe"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vafast/request-logger",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "API request logging middleware for Vafast",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.mjs",
|
|
@@ -32,15 +32,15 @@
|
|
|
32
32
|
"README.md",
|
|
33
33
|
"LICENSE"
|
|
34
34
|
],
|
|
35
|
-
"dependencies": {},
|
|
36
35
|
"devDependencies": {
|
|
37
36
|
"@types/node": "^22.15.30",
|
|
38
37
|
"rimraf": "^6.0.1",
|
|
39
38
|
"tsdown": "^0.19.0-beta.4",
|
|
40
39
|
"typescript": "^5.4.5",
|
|
41
|
-
"vafast": "^0.5.1"
|
|
40
|
+
"vafast": "^0.5.1",
|
|
41
|
+
"vitest": "^4.0.18"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"vafast": "^0.5.1"
|
|
45
45
|
}
|
|
46
|
-
}
|
|
46
|
+
}
|