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 -13
View File
@@ -8,6 +8,8 @@ import (
"regexp"
"strings"
"time"
"github.com/sirupsen/logrus"
)
// subDevPattern matches .s\d+ suffix for sub-devices.
@@ -39,7 +41,7 @@ func (c *MIoTClient) getOrCreateDevice(did string, info map[string]interface{})
spec, err := c.specParser.Parse(urn, model)
if err != nil {
c.lgr.Errorf("[MIoTClient] getOrCreateDevice: SPEC parse failed for %s (%s/%s): %v\n", did, urn, model, err)
logrus.Errorf("[MIoTClient] getOrCreateDevice: SPEC parse failed for %s (%s/%s): %v\n", did, urn, model, err)
return nil
}
@@ -169,7 +171,7 @@ func (c *MIoTClient) RefreshDevices(homeIDs []string) error {
} else {
c.deviceListCache[did] = cloneMap(info)
}
c.lgr.Debugf("[MIoTClient] Device added: %s (%v)\n", did, info["name"])
logrus.Debugf("[MIoTClient] Device added: %s (%v)\n", did, info["name"])
changed = true
} else {
// 检测并同步所有变更字段(name/model/nickname 等)
@@ -196,7 +198,7 @@ func (c *MIoTClient) RefreshDevices(homeIDs []string) error {
c.mu.Lock()
for _, did := range removed {
delete(c.devices, did)
c.lgr.Infof("[MIoTClient] Device removed: %s\n", did)
logrus.Infof("[MIoTClient] Device removed: %s\n", did)
}
c.mu.Unlock()
// 取消该设备在云/网关/局域网的订阅(三源均已清空 → 内部会退订)
@@ -224,7 +226,7 @@ func (c *MIoTClient) RefreshDevices(homeIDs []string) error {
}
deviceKey := fmt.Sprintf("%s_%s", c.uid, c.cloudServer)
if err := c.storage.Save("miot_devices", deviceKey, deviceListForStorage); err != nil {
c.lgr.Warnf("[MIoTClient] Warning: failed to save devices: %v\n", err)
logrus.Warnf("[MIoTClient] Warning: failed to save devices: %v\n", err)
}
}
@@ -233,7 +235,7 @@ func (c *MIoTClient) RefreshDevices(homeIDs []string) error {
c.ShowDevicesChangedNotify()
}
c.lgr.Infof("[MIoTClient] Devices refreshed: cloud=%d, cache=%d\n", len(cloudDevices), len(c.deviceListCache))
logrus.Infof("[MIoTClient] Devices refreshed: cloud=%d, cache=%d\n", len(cloudDevices), len(c.deviceListCache))
return nil
}
@@ -317,7 +319,7 @@ func (c *MIoTClient) deviceHomeID(did string) string {
// 5. homeScope 非空时只处理该家庭下的设备。
func (c *MIoTClient) pruneRemovedDevicesLocked(cloudDevices map[string]map[string]interface{}, homeScope map[string]struct{}) []string {
if len(cloudDevices) == 0 {
c.lgr.Warnf("[MIoTClient] empty cloud device list, skip removal detection\n")
logrus.Warnf("[MIoTClient] empty cloud device list, skip removal detection\n")
return nil
}
@@ -386,7 +388,7 @@ func (c *MIoTClient) aggregateDeviceState(did string) {
explicitFalse = true
}
} else if _, exists := info["online"]; exists {
c.lgr.Debugf("[MIoTClient] %s online type mismatch: %v (%T)\n", did, info["online"], info["online"])
logrus.Debugf("[MIoTClient] %s online type mismatch: %v (%T)\n", did, info["online"], info["online"])
}
}
@@ -401,7 +403,7 @@ func (c *MIoTClient) aggregateDeviceState(did string) {
} else {
online = true // 云列表里有但没 online 字段,假设在线
}
c.lgr.Debugf("[MIoTClient] %s no online field from cloud/gw/lan, fallback online=%v (cache=%v)\n",
logrus.Debugf("[MIoTClient] %s no online field from cloud/gw/lan, fallback online=%v (cache=%v)\n",
did, online, cacheInfo["online"])
}
@@ -413,7 +415,7 @@ func (c *MIoTClient) aggregateDeviceState(did string) {
func (c *MIoTClient) LoadDevices() error {
// Try to load from cache first
if err := c.loadCacheDevice(); err != nil {
c.lgr.Errorf("[MIoTClient] Failed to load from cache: %v\n", err)
logrus.Errorf("[MIoTClient] Failed to load from cache: %v\n", err)
}
// Then refresh from cloud
@@ -461,15 +463,15 @@ func (c *MIoTClient) ScheduleRefreshDevices(delay time.Duration) {
}
c.refreshCloudDevicesTimer = time.AfterFunc(delay, func() {
c.lgr.Infof("[MIoTClient] Refreshing devices...\n")
logrus.Infof("[MIoTClient] Refreshing devices...\n")
if err := c.RefreshDevices(c.homeIDs); err != nil {
c.lgr.Errorf("[MIoTClient] Refresh failed, will retry: %v\n", err)
logrus.Errorf("[MIoTClient] Refresh failed, will retry: %v\n", err)
// Retry after 60s
c.ScheduleRefreshDevices(RefreshCloudDevicesRetryDelay * time.Millisecond)
}
})
c.lgr.Infof("[MIoTClient] Device refresh scheduled in %v\n", delay)
logrus.Infof("[MIoTClient] Device refresh scheduled in %v\n", delay)
}
// Start starts the MIoTClient and schedules initial device load.
@@ -483,7 +485,7 @@ func (c *MIoTClient) Start() error {
// Schedule initial device load (delayed, aligns with REFRESH_CLOUD_DEVICES_DELAY=6 in Python)
c.ScheduleRefreshDevices(RefreshCloudDevicesDelay * time.Millisecond)
c.lgr.Infof("[MIoTClient] Started\n")
logrus.Infof("[MIoTClient] Started\n")
return nil
}