chenyc
2025-12-09 545c24c6a711d71b65f3d4e8122fee3837fb1edc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
 * Winston 日志配置模块
 * 提供详细的日志记录,用于问题排查和系统监控
 */
 
const winston = require('winston');
const path = require('path');
const fs = require('fs');
const config = require('./config.json');
 
// 创建日志目录
const logsDir = path.join(__dirname, 'logs');
if (!fs.existsSync(logsDir)) {
  fs.mkdirSync(logsDir, { recursive: true });
}
 
// 获取日志配置
const logConfig = config.logging || { level: 'info', maxFileSize: 10485760, maxFiles: 20 };
 
/**
 * 日志格式化函数 - 统一使用
 */
const formatLog = (info) => {
  let log = `${info.timestamp} [${info.level.toUpperCase()}]`;
  
  // 添加IP前缀(最高优先级)
  if (info.ip) {
    log += ` [IP: ${info.ip}]`;
  }
  
  // 添加设备前缀
  if (info.device) {
    log += ` [设备: ${info.device}]`;
  }
  
  if (info.module) {
    log += ` [${info.module}]`;
  }
  
  if (info.clientId) {
    log += ` [客户端#${info.clientId}]`;
  }
  
  log += `: ${info.message}`;
  
  // 添加元数据
  if (Object.keys(info).length > 0) {
    const metaKeys = ['module', 'device', 'clientId', 'timestamp', 'level', 'message', 'service', 'splat'];
    const meta = Object.entries(info)
      .filter(([key]) => !metaKeys.includes(key) && !key.startsWith('Symbol'))
      .map(([key, val]) => {
        if (typeof val === 'string' || typeof val === 'number' || typeof val === 'boolean') {
          return `${key}=${val}`;
        } else if (typeof val === 'object' && val !== null) {
          return `${key}=${JSON.stringify(val)}`;
        }
        return '';
      })
      .filter(m => m)
      .join(', ');
    
    if (meta) {
      log += ` {${meta}}`;
    }
  }
  
  // 添加堆栈跟踪
  if (info.stack) {
    log += `\n${info.stack}`;
  }
  
  return log;
};
 
/**
 * 创建 Winston Logger 实例
 */
const logger = winston.createLogger({
  level: process.env.LOG_LEVEL || logConfig.level || 'info',
  format: winston.format.combine(
    winston.format.timestamp({
      format: 'YYYY-MM-DD HH:mm:ss.SSS'
    }),
    winston.format.errors({ stack: true })
  ),
  defaultMeta: { service: 'dialysis-server' },
  transports: [
    // 控制台输出
    new winston.transports.Console({
      format: winston.format.combine(
        winston.format.colorize({ all: true }),
        winston.format.printf(info => formatLog(info))
      ),
      level: 'debug'
    }),
    
    // 所有日志文件
    new winston.transports.File({
      filename: path.join(logsDir, 'all.log'),
      maxsize: logConfig.maxFileSize || 10485760,
      maxFiles: logConfig.maxFiles || 20,
      tailable: true,
      format: winston.format.printf(info => formatLog(info))
    }),
    
    // 错误日志文件
    new winston.transports.File({
      filename: path.join(logsDir, 'error.log'),
      level: 'error',
      maxsize: logConfig.maxFileSize || 10485760,
      maxFiles: logConfig.maxFiles || 20,
      tailable: true,
      format: winston.format.printf(info => formatLog(info))
    })
  ]
});
 
// 创建自定义 Transport 过滤器函数
const createModuleTransport = (filename, moduleName) => {
  class ModuleFilterTransport extends winston.transports.File {
    log(info, callback) {
      if (info.module === moduleName) {
        super.log(info, callback);
      } else {
        if (callback) callback();
      }
    }
  }
  
  return new ModuleFilterTransport({
    filename: filename,
    maxsize: logConfig.maxFileSize || 10485760,
    maxFiles: logConfig.maxFiles || 20,
    tailable: true,
    format: winston.format.printf(info => formatLog(info))
  });
};
 
// 添加模块级别的 log 文件
logger.add(createModuleTransport(
  path.join(logsDir, 'tcp-connections.log'),
  'tcp'
));
 
logger.add(createModuleTransport(
  path.join(logsDir, 'data-parsing.log'),
  'parser'
));
 
logger.add(createModuleTransport(
  path.join(logsDir, 'mqtt.log'),
  'mqtt'
));
 
logger.add(createModuleTransport(
  path.join(logsDir, 'aliyun-iot.log'),
  'aliyun'
));
 
/**
 * 导出日志接口
 */
module.exports = {
  logger,
  
  // TCP 连接日志
  logTcpConnection: (ip, port, clientId) => {
    logger.info(`客户端连接建立`, {
      module: 'tcp',
      clientId,
      ip,
      port,
      timestamp: new Date().toISOString()
    });
  },
  
  logTcpDisconnection: (clientId, recordCount, deviceSN = null, ip = null) => {
    logger.info(`客户端连接断开`, {
      module: 'tcp',
      clientId,
      device: deviceSN,
      ip,
      recordCount,
      timestamp: new Date().toISOString()
    });
  },
  
  logTcpError: (clientId, error, deviceSN = null, ip = null) => {
    logger.error(`TCP 连接错误`, {
      module: 'tcp',
      clientId,
      device: deviceSN,
      ip,
      error: error.message,
      stack: error.stack
    });
  },
  
  // 数据解析日志
  logDataReceived: (clientId, deviceSN, dataSize) => {
    logger.debug(`收到设备数据`, {
      module: 'parser',
      clientId,
      device: deviceSN,
      dataSize: `${dataSize} bytes`
    });
  },
  
  logDataParsed: (clientId, deviceSN, recordCount, parameters) => {
    logger.info(`数据解析成功`, {
      module: 'parser',
      clientId,
      device: deviceSN,
      recordCount,
      parameterCount: Object.keys(parameters || {}).length
    });
  },
  
  logParsingError: (clientId, error) => {
    logger.error(`数据解析失败`, {
      module: 'parser',
      clientId,
      error: error.message,
      stack: error.stack
    });
  },
  
  // MQTT 日志
  logMqttConnecting: (deviceSN) => {
    logger.info(`正在连接 MQTT 服务`, {
      module: 'mqtt',
      device: deviceSN
    });
  },
  
  logMqttConnected: (deviceSN, brokerUrl) => {
    logger.info(`MQTT 连接成功`, {
      module: 'mqtt',
      device: deviceSN,
      broker: brokerUrl
    });
  },
  
  logMqttConnectionError: (deviceSN, error) => {
    logger.error(`MQTT 连接失败`, {
      module: 'mqtt',
      device: deviceSN,
      error: error.message
    });
  },
  
  logMqttPublishing: (deviceSN, topic, dataSize) => {
    logger.debug(`发布 MQTT 消息`, {
      module: 'mqtt',
      device: deviceSN,
      topic,
      size: `${dataSize} bytes`
    });
  },
  
  logMqttPublishError: (deviceSN, topic, error, payload) => {
    logger.error(`MQTT 发布失败`, {
      module: 'mqtt',
      device: deviceSN,
      topic,
      errorMessage: error.message || String(error),
      errorCode: error.code || 'UNKNOWN',
      payloadSize: payload ? `${payload.length} bytes` : 'N/A',
      payloadPreview: payload ? payload.substring(0, 150) : 'N/A',
      timestamp: new Date().toISOString()
    });
  },
  
  logMqttOffline: (deviceSN) => {
    logger.warn(`MQTT 离线`, {
      module: 'mqtt',
      device: deviceSN
    });
  },
  
  logMqttReconnecting: (deviceSN) => {
    logger.info(`MQTT 重新连接中`, {
      module: 'mqtt',
      device: deviceSN
    });
  },
 
  /**
   * 记录 MQTT 数据发送前的信息
   */
  logMqttDataSending: (deviceSN, topic, messageType, payload) => {
    logger.info(`发送 MQTT 消息前`, {
      module: 'mqtt',
      device: deviceSN,
      topic,
      type: messageType,
      size: `${payload.length} bytes`,
      payload: payload.substring(0, 200) // 记录前200个字符预览
    });
  },
 
  /**
   * 记录 MQTT 发布成功
   */
  logMqttPublishSuccess: (deviceSN, topic, messageType, payload) => {
    logger.info(`MQTT 消息发布成功`, {
      module: 'mqtt',
      device: deviceSN,
      topic,
      type: messageType,
      size: `${payload.length} bytes`,
      timestamp: new Date().toISOString()
    });
  },
 
  /**
   * 记录 MQTT 发布失败 - 增强版,包含发送内容
   */
  logMqttPublishFailure: (deviceSN, topic, messageType, error, payload) => {
    logger.error(`MQTT 消息发布失败`, {
      module: 'mqtt',
      device: deviceSN,
      topic,
      type: messageType,
      errorMessage: error.message || error,
      errorCode: error.code || 'UNKNOWN',
      size: `${payload.length} bytes`,
      payload: payload.substring(0, 200), // 记录前200个字符预览
      timestamp: new Date().toISOString()
    });
  },
  
  // 阿里云 IoT 日志
  logAliyunConnecting: (deviceSN) => {
    logger.info(`正在连接阿里云 IoT`, {
      module: 'aliyun',
      device: deviceSN
    });
  },
  
  logAliyunFetchingTriplet: (deviceSN) => {
    logger.info(`正在从 API 获取设备三元组`, {
      module: 'aliyun',
      device: deviceSN
    });
  },
  
  logAliyunTripletFetched: (deviceSN, triplet) => {
    logger.info(`设备三元组获取成功`, {
      module: 'aliyun',
      device: deviceSN,
      deviceName: triplet.deviceName,
      hasSecret: !!triplet.deviceSecret
    });
  },
  
  logAliyunTripletFetchError: (deviceSN, error) => {
    logger.warn(`从 API 获取三元组失败,尝试本地配置`, {
      module: 'aliyun',
      device: deviceSN,
      error: error.message || error
    });
  },
  
  logAliyunConnected: (deviceSN, deviceName) => {
    logger.info(`阿里云 IoT 连接成功`, {
      module: 'aliyun',
      device: deviceSN,
      deviceName
    });
  },
  
  logAliyunConnectionError: (deviceSN, error) => {
    logger.error(`阿里云 IoT 连接失败`, {
      module: 'aliyun',
      device: deviceSN,
      error: error.message
    });
  },
  
  logAliyunPublishing: (deviceSN, type, dataSize) => {
    logger.debug(`发布阿里云 IoT 消息`, {
      module: 'aliyun',
      device: deviceSN,
      type,
      size: `${dataSize} bytes`
    });
  },
  
  logAliyunPublishError: (deviceSN, type, error) => {
    logger.error(`阿里云 IoT 发布失败`, {
      module: 'aliyun',
      device: deviceSN,
      type,
      error: error.message
    });
  },
  
  logAliyunOffline: (deviceSN) => {
    logger.warn(`阿里云 IoT 离线`, {
      module: 'aliyun',
      device: deviceSN
    });
  },
  
  // 业务逻辑日志
  logStateEvent: (deviceSN, eventType, eventDescription) => {
    logger.info(`检测到设备状态事件`, {
      module: 'parser',
      device: deviceSN,
      eventType,
      eventDescription
    });
  },
  
  logDeviceRegistration: (deviceSN, ip) => {
    logger.info(`新设备已注册`, {
      device: deviceSN,
      ip,
      timestamp: new Date().toISOString()
    });
  },
  
  // TCP 原始消息日志
  logRawTcpMessage: (clientId, data, deviceSN = null, ip = null) => {
    // 记录原始二进制数据的十六进制表示和 ASCII 表示
    let hexStr = '';
    let asciiStr = '';
    
    for (let i = 0; i < data.length; i++) {
      const byte = data[i];
      hexStr += byte.toString(16).padStart(2, '0').toUpperCase() + ' ';
      // 可打印的 ASCII 字符直接显示,否则显示 .
      asciiStr += (byte >= 32 && byte <= 126) ? String.fromCharCode(byte) : '.';
    }
    
    // 如果数据较长,分段显示
    let message = `收到原始 TCP 消息 (${data.length} bytes)`;
    if (data.length > 100) {
      message += `\n  十六进制 (前100字节): ${hexStr.substring(0, 300)}...`;
      message += `\n  ASCII (前100字节): ${asciiStr.substring(0, 100)}...`;
    } else {
      message += `\n  十六进制: ${hexStr}`;
      message += `\n  ASCII: ${asciiStr}`;
    }
    
    logger.debug(message, {
      module: 'tcp',
      clientId,
      device: deviceSN,
      ip,
      totalBytes: data.length,
      rawData: data.toString('hex')  // 存储完整的十六进制数据供查询
    });
  },
 
  // 通用日志
  logInfo: (message, meta = {}) => {
    logger.info(message, meta);
  },
  
  logWarn: (message, meta = {}) => {
    logger.warn(message, meta);
  },
  
  logError: (message, error, meta = {}) => {
    logger.error(message, {
      ...meta,
      error: error.message,
      stack: error.stack
    });
  },
  
  logDebug: (message, meta = {}) => {
    logger.debug(message, meta);
  }
};