Files
bj_power/bj_power_wms/internal/mesclient/transport_logging.go
T
SunYF d92ca803d5 feat: 来料检处置项实体与复检、日排产达成对比、PLC/WMS 传输日志
- WMS 新增「检验处置项」实体(schema + ent 生成码),配套检验记录实体调整与复检接口,完善来料检/不良退货/入库检验前端
- MES 日排产:新增达成率统计与计划对比逻辑,调整日排产前端与流程卡/工序流
- 日志与传输:MES 新增 PLC 日志与 WMS 客户端传输日志,WMS 新增 MES 客户端传输日志
- MES 其他:扫码、工单/工件、AGV 客户端与告警、产品导入等逻辑完善及前端调整
- 工位终端:上线面板/作业面板、工件 handler 与 MES 客户端调整
- 配置与文档:更新 mes-api.yaml、三端构建产物与问题记录文档
2026-11-16 16:42:44 +08:00

66 lines
2.3 KiB
Go

// transport_logging.go WMS→MES 全量 HTTP 交互日志(2026-11-16 联调)。
// http.RoundTripper 装饰器:挂在 Client.hc 与 Ping 临时 client 上,
// 所有出口(getJSON/postJSON/Ping/LookupFinishedBySN/ListMaterialRequests 等)自动全覆盖。
// 记录:请求方法/完整URL/请求体(截断)、响应状态码/响应体(截断)、传输错误。
// 前缀 [WMS→MES]。
package mesclient
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
)
// loggingTransport 日志装饰 transport。
type loggingTransport struct{ base http.RoundTripper }
// FailHook MES 通讯失败钩子(2026-11-16):仅【传输层失败】(连不上/超时/断连)触发,
// HTTP 非 2xx 与业务码非 0 属正常业务响应不触发。main 启动时装配(落预警中心),
// 去重由回调实现方负责(窗口去重),本层只负责在错误发生时通知。
var FailHook func(ctx context.Context, method, url string, err error)
func newLoggingTransport() *loggingTransport {
return &loggingTransport{base: http.DefaultTransport}
}
func (t *loggingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
u := req.URL.String()
var reqBody []byte
if req.Body != nil {
reqBody, _ = io.ReadAll(req.Body)
_ = req.Body.Close()
req.Body = io.NopCloser(bytes.NewReader(reqBody))
}
logx.WithContext(req.Context()).Infof("[WMS→MES] --> %s %s body=%s", req.Method, u, truncateLogBytes(reqBody))
resp, err := t.base.RoundTrip(req)
if err != nil {
logx.WithContext(req.Context()).Errorf("[WMS→MES] <-- transport error %s err=%v", u, err)
if FailHook != nil {
FailHook(req.Context(), req.Method, u, err)
}
return resp, err
}
raw, rerr := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if rerr != nil {
logx.WithContext(req.Context()).Errorf("[WMS→MES] <-- read body error %s status=%d err=%v", u, resp.StatusCode, rerr)
}
resp.Body = io.NopCloser(bytes.NewReader(raw))
logx.WithContext(req.Context()).Infof("[WMS→MES] <-- HTTP %d %s body=%s", resp.StatusCode, u, truncateLogBytes(raw))
return resp, nil
}
// truncateLogBytes 日志防刷爆:超 2000 字节截断。
func truncateLogBytes(b []byte) string {
const max = 2000
if len(b) <= max {
return string(b)
}
return string(b[:max]) + fmt.Sprintf("...(截断,共%d字节)", len(b))
}