liufumingren d129103d0b refactor(log): 接口流水改用 Egg 日志落盘,整表下线 wms_system_log
背景:接口流水原本逐条写 wms_system_log 单表(生产 687,030 行、.ibd 28.8 GB、DATA_FREE 仅 5 MB),
删行无法把空间还给操作系统;且每个 needLog 请求都要多一次 INSERT。改为经 Egg 自定义 logger 平铺落盘,
整表 DROP 回收磁盘(生产实测释放 28.8 GB,/www 可用 20G→49G);日志目录同时由 /root/logs/wms
迁到 /www/wwwlogs/wms,不再占用系统盘。

新增 app/lib/api_log_sink.js:
- 一行一条 `HH:mm:ss {JSON}`:前缀是本地时区时分秒,只给人眼定位(tail/grep 的时间锚点),与行内
  created_at(UTC ISO 8601)是同一时刻;机器解析请用 created_at。
- 时间前缀刻意放在代码里而不是 config 的 formatter —— 生产 config 不参与 deploy、由人工维护,
  放代码才能随发布自动生效(此前已因「代码上线、配套配置没上」出现过接口日志静默丢失)。
- 对象/数组一律 JSON.stringify,避免调用方漏 stringify 时落盘成 "[object Object]"。
- customLogger.apiLog 缺失时每个 worker 显式告警一次:egg 的 getLogger(name) 对未声明的 name 返回
  null 而不抛错,不处理就会「既不落盘、也无任何报错」。

新增迁移 database/migrations/20260927090000-offline-wms-system-log.js:
- up 用 information_schema 探测后 DROP TABLE(幂等);该表对 wms_store 是出边外键,无需关外键检查。
- down 用原生 DDL 重建:queryInterface.createTable 在 MySQL 下默认字符集是 utf8mb3,且不会生成原始
  DDL 里的 created_at ON UPDATE CURRENT_TIMESTAMP,只有原生 DDL 才能忠实还原。
- 已在开发库跑通 up → down → up:重建后 14 列、主键、store_id 索引、外键、utf8mb4 均与下线前一致。

下线代码:删除 app/model/system_log.js、app/service/systems/system_log.js(60 天保留期清理服务)、
app/schedule/system_log_cleanup.js(每日 03:00 清理任务);移除 config.systemLog(writeToDb 回滚开关)
与 config.systemLogCleanup;api_log_sink 不再保留任何入库分支。

文档与脚本:AI_METHOD_ROUTING.md 第 6 节清单移除已下线的 systems/system_log.js(文件数 220→219);
script/method-output-regression.cjs 的回放样本来源由 `SELECT ... FROM wms_system_log` 改为读
wms-api.log(含按天轮转文件),并改为先整体 parse、失败再截到第一个 `{` 重试,兼容带/不带时间前缀
两种行(在真实 EggLogger 上验证:FileBufferTransport 每 1000ms flush,需等过 1s 才能读到)。

注:按日切割与 31 天清理由 egg 内置 egg-logrotator 自动完成(walkLoggerFile 覆盖 customLogger),
无需额外配 cron。
2026-09-27 03:05:15 +08:00

94 lines
2.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const Service = require('egg').Service;
const apiLogSink = require('../lib/api_log_sink');
class pdaService extends Service {
async callMethod(className, actionName, requestData, authHeader) {
try {
// 构建服务类路径
const servicePath = `${className}`;
const classs = servicePath.split('.');
let systemMethods = this.ctx.service.systemMethods;
for (let i = 0; i < classs.length; i++) {
const className = classs[i];
systemMethods = systemMethods[className];
}
// 检查服务是否存在
if (!systemMethods) {
throw new Error(`服务 ${servicePath} 不存在`);
}
const serviceInstance = systemMethods;
// 检查方法是否存在
if (typeof serviceInstance[actionName] !== 'function') {
throw new Error(`方法 ${actionName} 不存在`);
}
const result = {
message: '',
status: 401,
};
// 调用方法
const authPass = await serviceInstance.checkAuth(servicePath, actionName, authHeader);
const isLog = authPass.isLog;
if (authPass.status === 200) {
result.data = await serviceInstance[actionName](requestData);
result.status = 200;
} else {
result.message = authPass.message;
result.status = authPass.status;
}
return { isLog, result };
} catch (error) {
throw error;
}
}
validateParams(params) {
const required = [ 'method', 'v', 'timestamp' ];
for (const field of required) {
if (!params[field]) {
return false;
}
}
// 验证method格式
if (typeof params.method !== 'string' || !params.method.includes('.')) {
return false;
}
return true;
}
/**
* 保存接口日志。
*
* 经 Egg logger(apiLog)平铺写出到日志文件;wms_system_log 表已下线,不再有任何入库路径。
*
* @param {Object} logData 日志数据
* @param {number} startTime 开始时间戳
* @return {Promise<Object|undefined>} 写出的平铺记录
*/
async saveLog(logData, startTime) {
const {
ctx,
} = this;
try {
return await apiLogSink.write(ctx.app, {
logData,
startTime,
device: 'pda',
});
} catch (error) {
ctx.logger.error('保存日志失败: ' + error.message);
// 兜底:至少把原始数据落到 app 日志,避免整条丢失
ctx.logger.info('日志数据: ', JSON.stringify(logData));
}
}
}
module.exports = pdaService;