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:
+12
-19
@@ -6,8 +6,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"xiaomihome/logger"
|
||||
"xiaomihome/miot"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Client 高级封装客户端(包装 miot.MIoTClient + miot.MIoTHttpClient)
|
||||
@@ -18,7 +19,6 @@ type Client struct {
|
||||
mipsCloud *miot.MipsCloudClient
|
||||
specParser *miot.MIoTSpecParser
|
||||
uuid string
|
||||
lgr logger.Logger
|
||||
deviceMu sync.RWMutex
|
||||
devices map[string]*miot.MIoTDevice
|
||||
|
||||
@@ -95,16 +95,15 @@ func NewClient(ctx context.Context, cfg Config) (*Client, error) {
|
||||
}
|
||||
|
||||
c := &Client{
|
||||
cfg: cfg,
|
||||
inner: miotClient,
|
||||
http: httpClient,
|
||||
mipsCloud: mipsCloud,
|
||||
uuid: uuid,
|
||||
lgr: logger.Default(),
|
||||
specParser: miot.NewMIoTSpecParser("en", miot.NewMIoTStorage(cfg.DataDir), cfg.DataDir+"/specs"),
|
||||
devices: make(map[string]*miot.MIoTDevice),
|
||||
propSubs: make(map[string]string),
|
||||
eventSubs: make(map[string]string),
|
||||
cfg: cfg,
|
||||
inner: miotClient,
|
||||
http: httpClient,
|
||||
mipsCloud: mipsCloud,
|
||||
uuid: uuid,
|
||||
specParser: miot.NewMIoTSpecParser("en", miot.NewMIoTStorage(cfg.DataDir), cfg.DataDir+"/specs"),
|
||||
devices: make(map[string]*miot.MIoTDevice),
|
||||
propSubs: make(map[string]string),
|
||||
eventSubs: make(map[string]string),
|
||||
readbackModels: make(map[string]struct{}, len(cfg.ReadbackModels)),
|
||||
}
|
||||
for _, m := range cfg.ReadbackModels {
|
||||
@@ -124,12 +123,6 @@ func (c *Client) NeedReadback(model string) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
// SetLogger 设置日志记录器。
|
||||
func (c *Client) SetLogger(l logger.Logger) { c.lgr = l }
|
||||
|
||||
// Logger 返回日志记录器。
|
||||
func (c *Client) Logger() logger.Logger { return c.lgr }
|
||||
|
||||
// SetHomeIDs 动态设置 miot 层的家庭过滤,供后台定时 RefreshDevices 使用。
|
||||
func (c *Client) SetHomeIDs(homeIDs []string) { c.inner.SetHomeIDs(homeIDs) }
|
||||
|
||||
@@ -174,7 +167,7 @@ func (c *Client) getOrCreateDevice(did string, info map[string]interface{}) *mio
|
||||
if err == nil {
|
||||
spec = s
|
||||
} else {
|
||||
c.lgr.Warnf("[xiaomi] spec parse failed for %s (%s): %v", did, urn, err)
|
||||
logrus.Warnf("[xiaomi] spec parse failed for %s (%s): %v", did, urn, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user