refactor(logger): 日志改用 logrus 全局包级调用,移除 Logger 注入接口
为什么:并入宿主项目后须与宿主日志体系一致——宿主统一用 pkg/logger 初始化 logrus 全局实例并直接 logrus.Xxx 包级调用;包一层 Logger 接口会让 logrus 的 caller 定位(报告调用文件:行号)失准。 改动: - 删除 logger 包(Logger 接口 / Default / SetDefault,默认本就是 logrus.StandardLogger) - 11 个结构体移除 lgr logger.Logger 字段、SetLogger 方法及构造中的 lgr 初始化 - 192 处 lgr.Xxxf 调用改为 logrus.Xxxf,日志消息文本保持不变 - xiaomi.Client 移除 Logger() 访问器,air_conditioner 回读日志去掉判空调用直连 logrus - miot_i18n.go 两处标准库 log.Printf 改为 logrus.Errorf,统一日志出口 - go.mod:logrus 从 indirect 提升为直接依赖 - goimports/goformat 全树规范化:此前部分文件未 gofmt(单行 if、对齐),本次顺带 格式化,纯空白/换行变化,无逻辑改动 验证:go build / go vet / go test ./... 全部通过(bridge、miot、xiaomi、devices、specs)
This commit is contained in:
+15
-13
@@ -5,6 +5,8 @@ package miot
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// ============================================================================
|
||||
@@ -25,7 +27,7 @@ func (c *MIoTClient) SubProp(did string, handler func(map[string]interface{}, in
|
||||
subID := fmt.Sprintf("%p", handler)
|
||||
c.subTree.Sub(topic, handler, nil)
|
||||
|
||||
c.lgr.Debugf("[MIoTClient] Subscribed to property: %s (topic=%s, subID=%s)\n", did, topic, subID)
|
||||
logrus.Debugf("[MIoTClient] Subscribed to property: %s (topic=%s, subID=%s)\n", did, topic, subID)
|
||||
|
||||
c.mu.Unlock()
|
||||
|
||||
@@ -52,7 +54,7 @@ func (c *MIoTClient) UnsubProp(did string, subID string) {
|
||||
// TODO: Implement proper tracking of per-device subscription count
|
||||
// For now, just log
|
||||
|
||||
c.lgr.Debugf("[MIoTClient] Unsubscribed from property: %s (subID=%s)\n", did, subID)
|
||||
logrus.Debugf("[MIoTClient] Unsubscribed from property: %s (subID=%s)\n", did, subID)
|
||||
}
|
||||
|
||||
// SubEvent subscribes to device events.
|
||||
@@ -69,7 +71,7 @@ func (c *MIoTClient) SubEvent(did string, handler func(map[string]interface{}, i
|
||||
subID := fmt.Sprintf("%p", handler)
|
||||
c.subTree.Sub(topic, handler, nil)
|
||||
|
||||
c.lgr.Debugf("[MIoTClient] Subscribed to event: %s (topic=%s, subID=%s)\n", did, topic, subID)
|
||||
logrus.Debugf("[MIoTClient] Subscribed to event: %s (topic=%s, subID=%s)\n", did, topic, subID)
|
||||
|
||||
return subID
|
||||
}
|
||||
@@ -83,7 +85,7 @@ func (c *MIoTClient) UnsubEvent(did string, subID string) {
|
||||
// Remove from subTree by subID
|
||||
c.subTree.Unsub(subID)
|
||||
|
||||
c.lgr.Debugf("[MIoTClient] Unsubscribed from event: %s (subID=%s)\n", did, subID)
|
||||
logrus.Debugf("[MIoTClient] Unsubscribed from event: %s (subID=%s)\n", did, subID)
|
||||
}
|
||||
|
||||
// updateDeviceMsgSub updates device message subscription.
|
||||
@@ -155,7 +157,7 @@ func (c *MIoTClient) updateDeviceMsgSub(did string) {
|
||||
// Update subscription source
|
||||
c.subSourceList[did] = newSource
|
||||
|
||||
c.lgr.Debugf("[MIoTClient] updateDeviceMsgSub: %s source %s → %s (gw=%v/%v, lan=%v/%v, cloud=%v)\n",
|
||||
logrus.Debugf("[MIoTClient] updateDeviceMsgSub: %s source %s → %s (gw=%v/%v, lan=%v/%v, cloud=%v)\n",
|
||||
did, oldSource, newSource, gwOnline, gwPushAvailable, lanOnline, lanPushAvailable, cloudOnline)
|
||||
}
|
||||
|
||||
@@ -260,13 +262,13 @@ func (c *MIoTClient) handleMIPSMessage(did string, payload []byte) {
|
||||
// Fallback: binary MIPS protocol (used by gateway mipsLocal)
|
||||
msg := UnpackMipsMessage(payload)
|
||||
if msg == nil || msg.Payload == "" {
|
||||
c.lgr.Debugf("[MIoTClient] handleMIPSMessage: empty or invalid payload for %s (%d bytes)\n", did, len(payload))
|
||||
logrus.Debugf("[MIoTClient] handleMIPSMessage: empty or invalid payload for %s (%d bytes)\n", did, len(payload))
|
||||
return
|
||||
}
|
||||
|
||||
// Parse JSON from unpacked payload
|
||||
if err := json.Unmarshal([]byte(msg.Payload), ¶ms); err != nil {
|
||||
c.lgr.Errorf("[MIoTClient] handleMIPSMessage: JSON parse error for %s: %v\n", did, err)
|
||||
logrus.Errorf("[MIoTClient] handleMIPSMessage: JSON parse error for %s: %v\n", did, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -289,7 +291,7 @@ func (c *MIoTClient) dispatchMIPSMessage(did string, params map[string]interface
|
||||
} else if _, ok := params["eiid"]; ok {
|
||||
c.OnEventMsg(params, nil)
|
||||
} else {
|
||||
c.lgr.Debugf("[MIoTClient] handleMIPSMessage: unknown message format for %s: %v\n", did, params)
|
||||
logrus.Debugf("[MIoTClient] handleMIPSMessage: unknown message format for %s: %v\n", did, params)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -301,11 +303,11 @@ func (c *MIoTClient) handleMIPSStateMessage(did string, payload []byte) {
|
||||
// Fallback: binary MIPS protocol
|
||||
msg := UnpackMipsMessage(payload)
|
||||
if msg == nil || msg.Payload == "" {
|
||||
c.lgr.Debugf("[MIoTClient] handleMIPSStateMessage: empty payload for %s (%d bytes)\n", did, len(payload))
|
||||
logrus.Debugf("[MIoTClient] handleMIPSStateMessage: empty payload for %s (%d bytes)\n", did, len(payload))
|
||||
return
|
||||
}
|
||||
if err := json.Unmarshal([]byte(msg.Payload), ¶ms); err != nil {
|
||||
c.lgr.Errorf("[MIoTClient] handleMIPSStateMessage: JSON parse error for %s: %v\n", did, err)
|
||||
logrus.Errorf("[MIoTClient] handleMIPSStateMessage: JSON parse error for %s: %v\n", did, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -326,7 +328,7 @@ func (c *MIoTClient) handleMIPSStateMessage(did string, payload []byte) {
|
||||
}
|
||||
|
||||
if len(state) == 0 {
|
||||
c.lgr.Debugf("[MIoTClient] handleMIPSStateMessage: no state info in payload for %s: %v\n", did, params)
|
||||
logrus.Debugf("[MIoTClient] handleMIPSStateMessage: no state info in payload for %s: %v\n", did, params)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -372,7 +374,7 @@ func (c *MIoTClient) OnPropMsg(params map[string]interface{}, ctx interface{}) {
|
||||
}
|
||||
|
||||
if did == "" {
|
||||
c.lgr.Debugf("[MIoTClient] OnPropMsg: missing did in params=%v\n", params)
|
||||
logrus.Debugf("[MIoTClient] OnPropMsg: missing did in params=%v\n", params)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -424,7 +426,7 @@ func (c *MIoTClient) OnEventMsg(params map[string]interface{}, ctx interface{})
|
||||
}
|
||||
|
||||
if did == "" {
|
||||
c.lgr.Debugf("[MIoTClient] OnEventMsg: missing did in params=%v\n", params)
|
||||
logrus.Debugf("[MIoTClient] OnEventMsg: missing did in params=%v\n", params)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user