feat: AGV状态实时推送与受控异常处置 + rcs-dps状态接口独立配置与日志落盘修复
This commit is contained in:
@@ -12,9 +12,13 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"bj_power_wms/internal/netutil"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
// commTag 本机 IP 前缀(进程启动解析一次),断网排查时可一眼看出日志来自哪台机器。
|
||||
var commTag = netutil.Tag()
|
||||
|
||||
// loggingTransport 日志装饰 transport。
|
||||
type loggingTransport struct{ base http.RoundTripper }
|
||||
|
||||
@@ -35,11 +39,11 @@ func (t *loggingTransport) RoundTrip(req *http.Request) (*http.Response, error)
|
||||
_ = 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))
|
||||
logx.WithContext(req.Context()).Infof("[WMS→MES]"+commTag+" --> %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)
|
||||
logx.WithContext(req.Context()).Errorf("[WMS→MES]"+commTag+" <-- transport error %s err=%v", u, err)
|
||||
if FailHook != nil {
|
||||
FailHook(req.Context(), req.Method, u, err)
|
||||
}
|
||||
@@ -48,10 +52,10 @@ func (t *loggingTransport) RoundTrip(req *http.Request) (*http.Response, error)
|
||||
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)
|
||||
logx.WithContext(req.Context()).Errorf("[WMS→MES]"+commTag+" <-- 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))
|
||||
logx.WithContext(req.Context()).Infof("[WMS→MES]"+commTag+" <-- HTTP %d %s body=%s", resp.StatusCode, u, truncateLogBytes(raw))
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// Package netutil 提供本机 IP 缓存,供跨系统通信日志前缀使用(断网排查时能一眼看到是哪台机器发的日志)。
|
||||
package netutil
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
tagValue string
|
||||
)
|
||||
|
||||
// Tag 返回日志前缀片段,形如 [host=192.168.1.10];取不到非环回 IPv4 时返回 [host=?]。
|
||||
// 进程内只解析一次(sync.Once),不每次请求都枚举网卡;不使用正则,纯标准库。
|
||||
func Tag() string {
|
||||
once.Do(func() {
|
||||
ip := firstNonLoopbackIPv4()
|
||||
if ip == "" {
|
||||
ip = "?"
|
||||
}
|
||||
tagValue = "[host=" + ip + "]"
|
||||
})
|
||||
return tagValue
|
||||
}
|
||||
|
||||
// LocalIP 返回缓存的本机非环回 IPv4(取不到返回空串)。
|
||||
func LocalIP() string {
|
||||
Tag()
|
||||
ip := tagValue
|
||||
const p, s = "[host=", "]"
|
||||
if len(ip) > len(p)+len(s) {
|
||||
return ip[len(p) : len(ip)-len(s)]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func firstNonLoopbackIPv4() string {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
for _, a := range addrs {
|
||||
ipnet, ok := a.(*net.IPNet)
|
||||
if !ok || ipnet.IP.IsLoopback() {
|
||||
continue
|
||||
}
|
||||
v4 := ipnet.IP.To4()
|
||||
if v4 != nil {
|
||||
return v4.String()
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user