nomid2 commited on
Commit
f162614
·
verified ·
1 Parent(s): 511a39d

Upload logger.js

Browse files
Files changed (1) hide show
  1. src/lib/logger.js +75 -60
src/lib/logger.js CHANGED
@@ -29,11 +29,14 @@ class Logger {
29
  }
30
 
31
  /**
32
- * 格式化时间戳
33
  * @returns {string}
34
  */
35
  getTimestamp() {
36
- return new Date().toISOString()
 
 
 
37
  }
38
 
39
  /**
@@ -50,12 +53,13 @@ class Logger {
50
  return // 跳过低级别日志
51
  }
52
 
 
53
  const logEntry = {
54
- timestamp: this.getTimestamp(),
55
- level: level.toUpperCase(),
56
- category,
57
- event,
58
- message,
59
  ...data
60
  }
61
 
@@ -76,6 +80,37 @@ class Logger {
76
  }
77
  }
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  /**
80
  * 记录信息日志
81
  * @param {string} event - 事件类型
@@ -133,17 +168,17 @@ class Logger {
133
  })
134
 
135
  // 使用结构化日志记录请求开始
136
- this.log('INFO', 'REQUEST', 'REQUEST_START', '请求开始', {
137
- requestId,
138
- method,
139
- url,
140
- userAgent: headers['user-agent'] || 'unknown',
141
- contentType: headers['content-type'] || 'unknown',
142
- contentLength: headers['content-length'] || 'unknown',
143
- model: body.model || 'unknown',
144
- messageCount: Array.isArray(body.messages) ? body.messages.length : 0,
145
- hasImages: this.detectImages(body.messages),
146
- isStream: body.stream === true
147
  })
148
 
149
  return requestId
@@ -156,28 +191,26 @@ class Logger {
156
  * @param {Object} responseInfo - 响应信息
157
  */
158
  logRequestEnd(requestId, statusCode, responseInfo = {}) {
159
- const timestamp = this.getTimestamp()
160
  const activeRequest = this.activeRequests.get(requestId)
161
 
162
  if (!activeRequest) {
163
- console.warn(`[请求结束] 未找到请求ID: ${requestId}`)
164
  return
165
  }
166
 
167
  const duration = Date.now() - activeRequest.startTime
 
168
 
169
- const logData = {
 
170
  请求ID: requestId,
171
- : timestamp,
172
- 耗时: `${duration}ms`,
173
  状态码: statusCode,
174
- 成功: statusCode >= 200 && statusCode < 300,
175
  模型: activeRequest.model,
176
  流式请求: activeRequest.isStream,
177
  ...responseInfo
178
- }
179
-
180
- console.log(`[请求结束] ${JSON.stringify(logData)}`)
181
 
182
  // 清理活跃请求
183
  this.activeRequests.delete(requestId)
@@ -189,15 +222,10 @@ class Logger {
189
  * @param {number} imageCount - 图片数量
190
  */
191
  logImageProcessingStart(requestId, imageCount) {
192
- const timestamp = this.getTimestamp()
193
- const logData = {
194
  请求ID: requestId,
195
- 时间: timestamp,
196
- 事件: '图片处理开始',
197
  图片数量: imageCount
198
- }
199
-
200
- console.log(`[图片处理] ${JSON.stringify(logData)}`)
201
  }
202
 
203
  /**
@@ -207,18 +235,14 @@ class Logger {
207
  * @param {Object} detection - 检测结果
208
  */
209
  logLongImageDetection(requestId, imageIndex, detection) {
210
- const timestamp = this.getTimestamp()
211
- const logData = {
212
  请求ID: requestId,
213
- 时间: timestamp,
214
- 事件: '长图检测',
215
  图片索引: imageIndex,
216
  是否长图: detection.isLongImage ? '是' : '否',
217
- ��: `${detection.width}x${detection.height}`,
218
- 高宽比: detection.ratio?.toFixed(2)
219
- }
220
-
221
- console.log(`[图片处理] ${JSON.stringify(logData)}`)
222
  }
223
 
224
  /**
@@ -295,16 +319,11 @@ class Logger {
295
  * @param {string} mammouthModel - Mammouth平台模型名称
296
  */
297
  logModelCallStart(requestId, model, mammouthModel) {
298
- const timestamp = this.getTimestamp()
299
- const logData = {
300
  请求ID: requestId,
301
- 时间: timestamp,
302
- 事件: '模型调用开始',
303
  请求模型: model,
304
- 实际模型: mammouthModel,
305
- }
306
-
307
- console.log(`[模型调用] ${JSON.stringify(logData)}`)
308
  }
309
 
310
  /**
@@ -315,17 +334,13 @@ class Logger {
315
  * @param {number} duration - 调用耗时
316
  */
317
  logModelCallEnd(requestId, success, error = null, duration = null) {
318
- const timestamp = this.getTimestamp()
319
- const logData = {
320
  请求ID: requestId,
321
- 时间: timestamp,
322
- 事件: '模型调用结束',
323
- 成功: success ? '是' : '否',
324
  错误信息: success ? null : error,
325
- 耗时: duration ? `${duration}ms` : null
326
- }
327
-
328
- console.log(`[模型调用] ${JSON.stringify(logData)}`)
329
  }
330
 
331
  /**
 
29
  }
30
 
31
  /**
32
+ * 格式化时间戳(北京时间)
33
  * @returns {string}
34
  */
35
  getTimestamp() {
36
+ const now = new Date()
37
+ // 转换为北京时间 (UTC+8)
38
+ const beijingTime = new Date(now.getTime() + (8 * 60 * 60 * 1000))
39
+ return beijingTime.toISOString().replace('T', ' ').replace('Z', '').substring(0, 19)
40
  }
41
 
42
  /**
 
53
  return // 跳过低级别日志
54
  }
55
 
56
+ // 中文化日志字段
57
  const logEntry = {
58
+ 时间: this.getTimestamp(),
59
+ 级别: this.translateLevel(level.toUpperCase()),
60
+ 分类: this.translateCategory(category),
61
+ 事件: event,
62
+ 消息: message,
63
  ...data
64
  }
65
 
 
80
  }
81
  }
82
 
83
+ /**
84
+ * 翻译日志级别为中文
85
+ * @param {string} level - 英文级别
86
+ * @returns {string} 中文级别
87
+ */
88
+ translateLevel(level) {
89
+ const levelMap = {
90
+ 'ERROR': '错误',
91
+ 'WARN': '警告',
92
+ 'INFO': '信息',
93
+ 'DEBUG': '调试'
94
+ }
95
+ return levelMap[level] || level
96
+ }
97
+
98
+ /**
99
+ * 翻译日志分类为中文
100
+ * @param {string} category - 英文分类
101
+ * @returns {string} 中文分类
102
+ */
103
+ translateCategory(category) {
104
+ const categoryMap = {
105
+ 'APPLICATION': '应用',
106
+ 'REQUEST': '请求',
107
+ 'SYSTEM': '系统',
108
+ 'MODEL': '模型',
109
+ 'IMAGE': '图片'
110
+ }
111
+ return categoryMap[category] || category
112
+ }
113
+
114
  /**
115
  * 记录信息日志
116
  * @param {string} event - 事件类型
 
168
  })
169
 
170
  // 使用结构化日志记录请求开始
171
+ this.log('INFO', 'REQUEST', '请求开始', '收到新的API请求', {
172
+ 请求ID: requestId,
173
+ 方法: method,
174
+ 路径: url,
175
+ 用户代理: headers['user-agent'] || '未知',
176
+ 内容类型: headers['content-type'] || '未知',
177
+ 内容长度: headers['content-length'] || '未知',
178
+ 模型: body.model || '未知',
179
+ 消息数量: Array.isArray(body.messages) ? body.messages.length : 0,
180
+ 包含图片: this.detectImages(body.messages),
181
+ 流式请求: body.stream === true
182
  })
183
 
184
  return requestId
 
191
  * @param {Object} responseInfo - 响应信息
192
  */
193
  logRequestEnd(requestId, statusCode, responseInfo = {}) {
 
194
  const activeRequest = this.activeRequests.get(requestId)
195
 
196
  if (!activeRequest) {
197
+ this.logWarn('请求结束', `未找到请求ID: ${requestId}`)
198
  return
199
  }
200
 
201
  const duration = Date.now() - activeRequest.startTime
202
+ const isSuccess = statusCode >= 200 && statusCode < 300
203
 
204
+ // 使用结构化日志记录请求结束
205
+ this.log('INFO', 'REQUEST', '请求结束', '请求处理完成', {
206
  请求ID: requestId,
207
+ 毫秒: duration,
 
208
  状态码: statusCode,
209
+ 处理成功: isSuccess,
210
  模型: activeRequest.model,
211
  流式请求: activeRequest.isStream,
212
  ...responseInfo
213
+ })
 
 
214
 
215
  // 清理活跃请求
216
  this.activeRequests.delete(requestId)
 
222
  * @param {number} imageCount - 图片数量
223
  */
224
  logImageProcessingStart(requestId, imageCount) {
225
+ this.log('INFO', 'IMAGE', '图片处理开始', '开始处理上传的图片', {
 
226
  请求ID: requestId,
 
 
227
  图片数量: imageCount
228
+ })
 
 
229
  }
230
 
231
  /**
 
235
  * @param {Object} detection - 检测结果
236
  */
237
  logLongImageDetection(requestId, imageIndex, detection) {
238
+ this.log('INFO', 'IMAGE', '长图检测', '完成图片长度检测', {
 
239
  请求ID: requestId,
 
 
240
  图片索引: imageIndex,
241
  是否长图: detection.isLongImage ? '是' : '否',
242
+ 图片: `${detection.width}x${detection.height}`,
243
+ 高宽比: detection.ratio?.toFixed(2),
244
+ 检测阈值: detection.threshold
245
+ })
 
246
  }
247
 
248
  /**
 
319
  * @param {string} mammouthModel - Mammouth平台模型名称
320
  */
321
  logModelCallStart(requestId, model, mammouthModel) {
322
+ this.log('INFO', 'MODEL', '模型调用开始', '开始调用AI模型', {
 
323
  请求ID: requestId,
 
 
324
  请求模型: model,
325
+ 实际模型: mammouthModel
326
+ })
 
 
327
  }
328
 
329
  /**
 
334
  * @param {number} duration - 调用耗时
335
  */
336
  logModelCallEnd(requestId, success, error = null, duration = null) {
337
+ this.log(success ? 'INFO' : 'ERROR', 'MODEL', '模型调用结束',
338
+ success ? '模型调用成功' : '模型调用失败', {
339
  请求ID: requestId,
340
+ 调用成功: success ? '是' : '否',
 
 
341
  错误信息: success ? null : error,
342
+ 耗时毫秒: duration
343
+ })
 
 
344
  }
345
 
346
  /**