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:
2026-09-15 20:16:26 +08:00
parent 7ae32bd012
commit 2b7de69403
46 changed files with 1166 additions and 903 deletions
+15 -27
View File
@@ -13,8 +13,7 @@ import (
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
"xiaomihome/logger"
"github.com/sirupsen/logrus"
)
// ============================================================================
@@ -25,8 +24,8 @@ const (
mipsQoS = 2
mipsReconnectIntervalMin = 10.0 // seconds
mipsReconnectIntervalMax = 600.0 // seconds
mipsSubInterval = 1 // second
mipsMQTTInterval = 1 // second
mipsSubInterval = 1 // second
mipsMQTTInterval = 1 // second
uint32max = 0xFFFFFFFF
MipsRequestTimeoutDefault = 10000 // milliseconds
)
@@ -218,7 +217,6 @@ type MipsBroadcast struct {
// mipsClient is the concrete MIPS client using paho MQTT.
type mipsClient struct {
mu sync.Mutex
lgr logger.Logger
clientID string
host string
port int
@@ -286,7 +284,6 @@ func newMipsClient(clientID, host string, port int, username, password, caFile,
caFile: caFile,
certFile: certFile,
keyFile: keyFile,
lgr: logger.Default(),
seedID: rand.Intn(uint32max),
pendingSubs: make(map[string]int),
subscriptions: make(map[string]*MipsBroadcast),
@@ -299,9 +296,6 @@ func newMipsClient(clientID, host string, port int, username, password, caFile,
return c
}
// SetLogger sets a custom logger for mipsClient.
func (c *mipsClient) SetLogger(l logger.Logger) { c.lgr = l }
// connect starts the MQTT connection.
func (c *mipsClient) connect(mqttClient MQTTClient) error {
c.mu.Lock()
@@ -369,7 +363,7 @@ func (c *mipsClient) subscribe(topic string, handler func(topic string, payload
defer c.subMu.Unlock()
if c.subscriptions[topic] != nil {
c.lgr.Debugf("[mips] re-register subscription: %s", topic)
logrus.Debugf("[mips] re-register subscription: %s", topic)
}
c.subscriptions[topic] = &MipsBroadcast{
Topic: topic,
@@ -394,7 +388,7 @@ func (c *mipsClient) unsubscribe(topic string) error {
if c.mqttConn != nil && c.mqttConn.IsConnected() {
if err := c.mqttConn.Unsubscribe(topic); err != nil {
c.lgr.Debugf("[mips] unsubscribe error %s: %v", topic, err)
logrus.Debugf("[mips] unsubscribe error %s: %v", topic, err)
}
}
return nil
@@ -534,7 +528,7 @@ func (c *mipsClient) flushPendingSubs() {
for topic, count := range c.pendingSubs {
if count > 3 {
delete(c.pendingSubs, topic)
c.lgr.Debugf("[mips] retry sub exceeded: %s", topic)
logrus.Debugf("[mips] retry sub exceeded: %s", topic)
continue
}
batch[topic] = byte(mipsQoS)
@@ -549,7 +543,7 @@ func (c *mipsClient) flushPendingSubs() {
c.pendingSubMu.Lock()
for t := range batch {
delete(c.pendingSubs, t)
c.lgr.Debugf("[mips] sub success: %s", t)
logrus.Debugf("[mips] sub success: %s", t)
}
hasMore := len(c.pendingSubs) > 0
c.pendingSubMu.Unlock()
@@ -558,7 +552,7 @@ func (c *mipsClient) flushPendingSubs() {
c.flushPendingSubs()
}
} else {
c.lgr.Debugf("[mips] batch sub error: %v", err)
logrus.Debugf("[mips] batch sub error: %v", err)
c.pendingSubMu.Lock()
for t := range batch {
c.pendingSubs[t] = c.pendingSubs[t] + 1
@@ -661,13 +655,13 @@ func (c *mipsClient) tryReconnect(immediate bool) {
c.reconnectInterval = math.Min(c.reconnectInterval*2, mipsReconnectIntervalMax)
}
interval = time.Duration(c.reconnectInterval) * time.Second
c.lgr.Debugf("[mips] reconnect after %v", interval)
logrus.Debugf("[mips] reconnect after %v", interval)
}
c.reconnectTimer = time.AfterFunc(interval, func() {
if c.mqttConn != nil {
if err := c.mqttConn.Connect(); err != nil {
c.lgr.Debugf("[mips] reconnect failed: %v", err)
logrus.Debugf("[mips] reconnect failed: %v", err)
c.tryReconnect(false)
}
}
@@ -951,8 +945,6 @@ type pahoMQTTClient struct {
certFile string
keyFile string
lgr logger.Logger
onConnect func()
onDisconnect func()
onMessage func(topic string, payload []byte)
@@ -971,14 +963,10 @@ func newPahoMQTTClient(clientID, host string, port int, username, password, caFi
caFile: caFile,
certFile: certFile,
keyFile: keyFile,
lgr: logger.Default(),
}
return c
}
// SetLogger sets a custom logger for pahoMQTTClient.
func (p *pahoMQTTClient) SetLogger(l logger.Logger) { p.lgr = l }
func (p *pahoMQTTClient) Connect() error {
opts := mqtt.NewClientOptions()
opts.AddBroker(fmt.Sprintf("ssl://%s:%d", p.host, p.port))
@@ -990,7 +978,7 @@ func (p *pahoMQTTClient) Connect() error {
opts.SetCleanSession(true)
opts.OnConnect = func(c mqtt.Client) {
p.lgr.Debugf("[mips] MQTT connected to %s:%d", p.host, p.port)
logrus.Debugf("[mips] MQTT connected to %s:%d", p.host, p.port)
p.connected = true
if p.onConnect != nil {
p.onConnect()
@@ -998,7 +986,7 @@ func (p *pahoMQTTClient) Connect() error {
}
opts.OnConnectionLost = func(c mqtt.Client, err error) {
p.lgr.Debugf("[mips] MQTT connection lost: %v", err)
logrus.Debugf("[mips] MQTT connection lost: %v", err)
p.connected = false
if p.onDisconnect != nil {
p.onDisconnect()
@@ -1006,7 +994,7 @@ func (p *pahoMQTTClient) Connect() error {
}
opts.DefaultPublishHandler = func(c mqtt.Client, msg mqtt.Message) {
p.lgr.Debugf("[mips] MQTT message: %s (%d bytes) data:%s", msg.Topic(), len(msg.Payload()), string(msg.Payload()))
logrus.Debugf("[mips] MQTT message: %s (%d bytes) data:%s", msg.Topic(), len(msg.Payload()), string(msg.Payload()))
if p.onMessage != nil {
p.onMessage(msg.Topic(), msg.Payload())
}
@@ -1018,7 +1006,7 @@ func (p *pahoMQTTClient) Connect() error {
return token.Error()
}
p.lgr.Debugf("[mips] MQTT connected to %s:%d as %s", p.host, p.port, p.clientID)
logrus.Debugf("[mips] MQTT connected to %s:%d as %s", p.host, p.port, p.clientID)
return nil
}
@@ -1075,7 +1063,7 @@ func (p *pahoMQTTClient) Unsubscribe(topic string) error {
if token.Wait() && token.Error() != nil {
return token.Error()
}
p.lgr.Debugf("[mips] unsub success: %s", topic)
logrus.Debugf("[mips] unsub success: %s", topic)
return nil
}