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
+13 -20
View File
@@ -7,7 +7,7 @@ import (
"strings"
"sync"
"xiaomihome/logger"
"github.com/sirupsen/logrus"
)
// ============================================================================
@@ -53,12 +53,9 @@ type MIoTDevice struct {
// Subscription lists (aligned with Python _device_state_sub_list / _value_sub_list)
deviceStateSubList map[string]map[string]func(string, interface{})
valueSubList map[string]map[string]func(map[string]interface{}, interface{})
subID int
subIDMu sync.Mutex
// === Logger ===
lgr logger.Logger
valueSubList map[string]map[string]func(map[string]interface{}, interface{})
subID int
subIDMu sync.Mutex
}
// NewMIoTDevice creates a new MIoTDevice.
@@ -68,12 +65,11 @@ type MIoTDevice struct {
// specInstance: SPEC instance (can be nil, lazy loaded)
func NewMIoTDevice(miotClient *MIoTClient, deviceInfo map[string]interface{}, specInstance *MIoTSpecInstance) *MIoTDevice {
dev := &MIoTDevice{
miotClient: miotClient,
specInstance: specInstance,
deviceStateSubList: make(map[string]map[string]func(string, interface{})),
valueSubList: make(map[string]map[string]func(map[string]interface{}, interface{})),
subDevices: make(map[int]*MIoTDevice),
lgr: logger.Default(),
miotClient: miotClient,
specInstance: specInstance,
deviceStateSubList: make(map[string]map[string]func(string, interface{})),
valueSubList: make(map[string]map[string]func(map[string]interface{}, interface{})),
subDevices: make(map[int]*MIoTDevice),
}
if specInstance != nil {
@@ -140,9 +136,6 @@ func NewMIoTDevice(miotClient *MIoTClient, deviceInfo map[string]interface{}, sp
return dev
}
// SetLogger sets a custom logger for MIoTDevice.
func (d *MIoTDevice) SetLogger(l logger.Logger) { d.lgr = l }
// LoadFromCloud creates a MIoTDevice from cloud device data.
// Aligns with Python: populating device_info from cloud response.
func LoadFromCloud(miotClient *MIoTClient, cloudData map[string]interface{}) *MIoTDevice {
@@ -206,7 +199,7 @@ func (d *MIoTDevice) SetSpecInstance(specInstance *MIoTSpecInstance) {
// Returns: subscription ID for unsubscribing.
func (d *MIoTDevice) SubProp(siid, piid int, handler func(map[string]interface{}, interface{})) string {
if d.miotClient == nil {
d.lgr.Warnf("[MIoTDevice] Warning: miotClient is nil, cannot subscribe to property\n")
logrus.Warnf("[MIoTDevice] Warning: miotClient is nil, cannot subscribe to property\n")
return ""
}
@@ -229,7 +222,7 @@ func (d *MIoTDevice) SubProp(siid, piid int, handler func(map[string]interface{}
// Aligns with Python MIoTDevice.unsub_prop.
func (d *MIoTDevice) UnsubProp(subID string) {
if d.miotClient == nil {
d.lgr.Warnf("[MIoTDevice] Warning: miotClient is nil, cannot unsubscribe from property\n")
logrus.Warnf("[MIoTDevice] Warning: miotClient is nil, cannot unsubscribe from property\n")
return
}
@@ -255,7 +248,7 @@ func (d *MIoTDevice) UnsubProp(subID string) {
// Returns: subscription ID for unsubscribing.
func (d *MIoTDevice) SubEvent(siid, eiid int, handler func(map[string]interface{}, interface{})) string {
if d.miotClient == nil {
d.lgr.Warnf("[MIoTDevice] Warning: miotClient is nil, cannot subscribe to event\n")
logrus.Warnf("[MIoTDevice] Warning: miotClient is nil, cannot subscribe to event\n")
return ""
}
@@ -278,7 +271,7 @@ func (d *MIoTDevice) SubEvent(siid, eiid int, handler func(map[string]interface{
// Aligns with Python MIoTDevice.unsub_event.
func (d *MIoTDevice) UnsubEvent(subID string) {
if d.miotClient == nil {
d.lgr.Warnf("[MIoTDevice] Warning: miotClient is nil, cannot unsubscribe from event\n")
logrus.Warnf("[MIoTDevice] Warning: miotClient is nil, cannot unsubscribe from event\n")
return
}