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
+16 -26
View File
@@ -17,7 +17,7 @@ import (
"sync"
"time"
"xiaomihome/logger"
"github.com/sirupsen/logrus"
)
// ============================================================================
@@ -45,8 +45,8 @@ type MIoTSpecService struct {
Type string
Description string
DescriptionTrans string
NeedFilter bool
Proprietary bool
NeedFilter bool
Proprietary bool
Properties []*MIoTSpecProperty
Actions []*MIoTSpecAction
Events []*MIoTSpecEvent
@@ -118,13 +118,12 @@ type MIoTSpecEvent struct {
// MIoTSpecParser wraps SPEC parsing logic.
type MIoTSpecParser struct {
lang string
storage *MIoTStorage
specDir string
stdLib *SpecStdLib
lgr logger.Logger
initDone bool
initMu sync.Mutex
lang string
storage *MIoTStorage
specDir string
stdLib *SpecStdLib
initDone bool
initMu sync.Mutex
}
// NewMIoTSpecParser creates a new MIoTSpecParser.
@@ -137,14 +136,10 @@ func NewMIoTSpecParser(lang string, storage *MIoTStorage, specDir string) *MIoTS
storage: storage,
specDir: specDir,
stdLib: NewSpecStdLib(lang),
lgr: logger.Default(),
}
return p
}
// SetLogger sets a custom logger for MIoTSpecParser.
func (p *MIoTSpecParser) SetLogger(l logger.Logger) { p.lgr = l }
// Init initializes the parser, loading or refreshing the standard library.
func (p *MIoTSpecParser) Init() error {
p.initMu.Lock()
@@ -168,7 +163,7 @@ func (p *MIoTSpecParser) Init() error {
ts, _ = v.Int64()
}
if ts > 0 && time.Now().Unix()-ts < SPEC_STD_LIB_EFFECTIVE_TIME {
p.lgr.Debugf("[spec] using cached spec std lib, ts=%d", ts)
logrus.Debugf("[spec] using cached spec std lib, ts=%d", ts)
p.stdLib.Load(dataMap)
p.initDone = true
return nil
@@ -180,12 +175,12 @@ func (p *MIoTSpecParser) Init() error {
// Refresh from cloud
if err := p.stdLib.Refresh(); err != nil {
p.lgr.Debugf("[spec] refresh std lib failed: %v", err)
logrus.Debugf("[spec] refresh std lib failed: %v", err)
// Try stale cache as fallback
if cacheMap, ok := stdLibCache.(map[string]interface{}); ok {
if dataField, ok := cacheMap["data"]; ok {
if dataMap, ok := dataField.(map[string]interface{}); ok {
p.lgr.Debugf("[spec] using stale cached spec std lib")
logrus.Debugf("[spec] using stale cached spec std lib")
p.stdLib.Load(dataMap)
p.initDone = true
return nil
@@ -282,7 +277,7 @@ func (p *MIoTSpecParser) downloadSpecFile(urn, filePath string) error {
return fmt.Errorf("write %s: %w", filePath, err)
}
p.lgr.Debugf("[spec] downloaded spec: %s -> %s", urn, filePath)
logrus.Debugf("[spec] downloaded spec: %s -> %s", urn, filePath)
return nil
}
@@ -574,7 +569,6 @@ func (p *MIoTSpecParser) skipProprietary(item map[string]interface{}) bool {
// SpecStdLib is the MIoT-Spec-V2 standard library for multi-language descriptions.
type SpecStdLib struct {
lang string
lgr logger.Logger
devices map[string]map[string]string
services map[string]map[string]string
properties map[string]map[string]string
@@ -590,7 +584,6 @@ func NewSpecStdLib(lang string) *SpecStdLib {
}
s := &SpecStdLib{
lang: lang,
lgr: logger.Default(),
devices: make(map[string]map[string]string),
services: make(map[string]map[string]string),
properties: make(map[string]map[string]string),
@@ -601,9 +594,6 @@ func NewSpecStdLib(lang string) *SpecStdLib {
return s
}
// SetLogger sets a custom logger for SpecStdLib.
func (s *SpecStdLib) SetLogger(l logger.Logger) { s.lgr = l }
// Load loads standard library data from a parsed map.
func (s *SpecStdLib) Load(data map[string]interface{}) {
if data == nil {
@@ -703,7 +693,7 @@ func (s *SpecStdLib) Refresh() error {
var lastErr error
for attempt := 0; attempt < 3; attempt++ {
if attempt > 0 {
s.lgr.Debugf("[spec] retry std lib refresh, attempt %d", attempt)
logrus.Debugf("[spec] retry std lib refresh, attempt %d", attempt)
time.Sleep(time.Duration(attempt) * time.Second)
}
@@ -736,7 +726,7 @@ func (s *SpecStdLib) Refresh() error {
url := fmt.Sprintf("https://cdn.cnbj1.fds.api.mi-img.com/res-conf/xiaomi-home/std_ex_%s.json", name)
extData, err := s.fetchJSONMapMap(httpClient, url)
if err != nil {
s.lgr.Debugf("[spec] get external std lib failed, %s: %v", name, err)
logrus.Debugf("[spec] get external std lib failed, %s: %v", name, err)
continue
}
if extData != nil {
@@ -776,7 +766,7 @@ func (s *SpecStdLib) Refresh() error {
if len(errs) > 0 {
lastErr = fmt.Errorf("std lib refresh errors: %v", errs)
s.lgr.Debugf("[spec] std lib refresh error: %v", lastErr)
logrus.Debugf("[spec] std lib refresh error: %v", lastErr)
}
}