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:
+6
-12
@@ -12,8 +12,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/mdns"
|
||||
|
||||
"xiaomihome/logger"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// ============================================================================
|
||||
@@ -166,7 +165,6 @@ type MipsService struct {
|
||||
mu sync.Mutex
|
||||
services map[string]*MipsServiceData // keyed by groupID
|
||||
subs map[subKey]MipsServiceChangeHandler
|
||||
lgr logger.Logger
|
||||
|
||||
stopCh chan struct{}
|
||||
}
|
||||
@@ -182,14 +180,10 @@ func NewMipsService() *MipsService {
|
||||
s := &MipsService{
|
||||
services: make(map[string]*MipsServiceData),
|
||||
subs: make(map[subKey]MipsServiceChangeHandler),
|
||||
lgr: logger.Default(),
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// SetLogger sets a custom logger for MipsService.
|
||||
func (s *MipsService) SetLogger(l logger.Logger) { s.lgr = l }
|
||||
|
||||
// Init starts the mDNS browser in a background goroutine.
|
||||
// Safe to call multiple times; subsequent calls after the first are no-ops
|
||||
// unless preceded by Deinit().
|
||||
@@ -203,7 +197,7 @@ func (s *MipsService) Init() {
|
||||
s.mu.Unlock()
|
||||
|
||||
go s.runBrowser()
|
||||
s.lgr.Infof("[mdns] mips service browser started")
|
||||
logrus.Infof("[mdns] mips service browser started")
|
||||
}
|
||||
|
||||
// Deinit stops the browser goroutine and clears all cached services and
|
||||
@@ -225,7 +219,7 @@ func (s *MipsService) Deinit() {
|
||||
s.subs = make(map[subKey]MipsServiceChangeHandler)
|
||||
s.mu.Unlock()
|
||||
|
||||
s.lgr.Infof("[mdns] mips service deinitialized")
|
||||
logrus.Infof("[mdns] mips service deinitialized")
|
||||
}
|
||||
|
||||
// GetServices returns cached services. When groupID is non-empty, only that
|
||||
@@ -289,7 +283,7 @@ func (s *MipsService) runBrowser() {
|
||||
// Run the blocking Query in a goroutine so we can detect stop.
|
||||
go func() {
|
||||
if err := mdns.Query(params); err != nil {
|
||||
s.lgr.Debugf("[mdns] query error: %v", err)
|
||||
logrus.Debugf("[mdns] query error: %v", err)
|
||||
}
|
||||
close(entriesCh)
|
||||
}()
|
||||
@@ -311,7 +305,7 @@ func (s *MipsService) runBrowser() {
|
||||
func (s *MipsService) handleEntry(entry *mdns.ServiceEntry) {
|
||||
svc, err := entryToServiceData(entry)
|
||||
if err != nil {
|
||||
s.lgr.Debugf("[mdns] skip entry: %v", err)
|
||||
logrus.Debugf("[mdns] skip entry: %v", err)
|
||||
return
|
||||
}
|
||||
if !svc.validService() {
|
||||
@@ -339,7 +333,7 @@ func (s *MipsService) handleEntry(entry *mdns.ServiceEntry) {
|
||||
// callSubsLocked notifies all subscribers whose groupID matches.
|
||||
// Caller MUST hold s.mu.
|
||||
func (s *MipsService) callSubsLocked(groupID string, state MipsServiceState, data map[string]interface{}) {
|
||||
s.lgr.Infof("[mdns] service change: groupID=%s, state=%s", groupID, state)
|
||||
logrus.Infof("[mdns] service change: groupID=%s, state=%s", groupID, state)
|
||||
for sk, handler := range s.subs {
|
||||
if sk.groupID == groupID || sk.groupID == "*" {
|
||||
go handler(groupID, state, data)
|
||||
|
||||
Reference in New Issue
Block a user