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
+3 -1
View File
@@ -6,6 +6,8 @@ import (
"xiaomihome/xiaomi"
"xiaomihome/xiaomi/devices"
"github.com/sirupsen/logrus"
)
// ── 账号 / Token / 家庭 ──
@@ -66,7 +68,7 @@ func (b *Bridge) RefreshDevices(ctx context.Context, homeIDs ...int64) error {
alive[info.DID] = struct{}{}
}
if removed := pruneControllers(b.dv, alive); len(removed) > 0 {
b.lgr.Infof("[bridge] %d: %d device(s) removed from Mi Home, controllers dropped: %v",
logrus.Infof("[bridge] %d: %d device(s) removed from Mi Home, controllers dropped: %v",
b.id, len(removed), removed)
}
+113 -55
View File
@@ -17,23 +17,24 @@ import (
"fmt"
"sync"
"xiaomihome/logger"
"xiaomihome/xiaomi"
"xiaomihome/xiaomi/devices"
"github.com/sirupsen/logrus"
)
// ── 类型别名:业务层不需要 import devices ──
type (
ACProps = devices.ACProps
LightProps = devices.LightProps
FanProps = devices.FanProps
HumidifierProps = devices.HumidifierProps
HeaterProps = devices.HeaterProps
ACProps = devices.ACProps
LightProps = devices.LightProps
FanProps = devices.FanProps
HumidifierProps = devices.HumidifierProps
HeaterProps = devices.HeaterProps
VacuumProps = devices.VacuumProps
SwitchProps = devices.SwitchProps
SpeakerProps = devices.SpeakerProps
PlugProps = devices.PlugProps
SwitchProps = devices.SwitchProps
SpeakerProps = devices.SpeakerProps
PlugProps = devices.PlugProps
)
// OccupancyProps 人体传感器状态(有人/无人)。
@@ -112,7 +113,6 @@ func (p *Pool) Close() {
type Bridge struct {
id int64
client *xiaomi.Client
lgr logger.Logger
mu sync.RWMutex
dv map[string]interface{} // DID → devices.Xxx interface
@@ -137,7 +137,6 @@ func newBridge(ctx context.Context, id int64, uuid string, auth xiaomi.AuthInfo,
b := &Bridge{
id: id,
client: client,
lgr: logger.Default(),
dv: make(map[string]interface{}),
}
@@ -151,22 +150,19 @@ func newBridge(ctx context.Context, id int64, uuid string, auth xiaomi.AuthInfo,
d, createErr := devices.Create(client, info)
if createErr != nil || d == nil {
if createErr != nil {
b.lgr.Warnf("[bridge] %d: create device %s (%s) failed: %v", id, info.DID, info.Model, createErr)
logrus.Warnf("[bridge] %d: create device %s (%s) failed: %v", id, info.DID, info.Model, createErr)
} else {
b.lgr.Debugf("[bridge] %d: device %s (%s) unsupported, skipped", id, info.DID, info.Model)
logrus.Debugf("[bridge] %d: device %s (%s) unsupported, skipped", id, info.DID, info.Model)
}
continue
}
b.dv[info.DID] = d
}
b.lgr.Infof("[bridge] %d: %d devices", id, len(b.dv))
logrus.Infof("[bridge] %d: %d devices", id, len(b.dv))
return b, nil
}
// SetLogger 设置日志记录器。
func (b *Bridge) SetLogger(l logger.Logger) { b.lgr = l }
func (b *Bridge) Close() error { return b.client.Close() }
// Devices 返回设备列表(DID → 类型名)。
@@ -195,20 +191,34 @@ func pruneControllers(dv map[string]interface{}, alive map[string]struct{}) []st
func deviceTypeName(d interface{}) string {
switch d.(type) {
case devices.AirConditioner: return devices.TypeAirConditioner
case devices.Light: return devices.TypeLight
case devices.Switch: return devices.TypeSwitch
case devices.Fan: return devices.TypeFan
case devices.Cover: return devices.TypeCover
case devices.Humidifier: return devices.TypeHumidifier
case devices.Vacuum: return devices.TypeVacuum
case devices.WaterHeater: return devices.TypeWaterHeater
case devices.TempHumiditySensor: return devices.TypeTempHumidSensor
case devices.OccupancySensor: return devices.TypeOccupancySensor
case devices.Speaker: return devices.TypeSpeaker
case devices.Gateway: return devices.TypeGateway
case devices.Plug: return devices.TypePlug
default: return "unknown"
case devices.AirConditioner:
return devices.TypeAirConditioner
case devices.Light:
return devices.TypeLight
case devices.Switch:
return devices.TypeSwitch
case devices.Fan:
return devices.TypeFan
case devices.Cover:
return devices.TypeCover
case devices.Humidifier:
return devices.TypeHumidifier
case devices.Vacuum:
return devices.TypeVacuum
case devices.WaterHeater:
return devices.TypeWaterHeater
case devices.TempHumiditySensor:
return devices.TypeTempHumidSensor
case devices.OccupancySensor:
return devices.TypeOccupancySensor
case devices.Speaker:
return devices.TypeSpeaker
case devices.Gateway:
return devices.TypeGateway
case devices.Plug:
return devices.TypePlug
default:
return "unknown"
}
}
@@ -216,85 +226,133 @@ func deviceTypeName(d interface{}) string {
func (b *Bridge) AC(did string) (devices.AirConditioner, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s not found", did)
}
ac, ok := d.(devices.AirConditioner)
if !ok { return nil, fmt.Errorf("bridge: device %s is not an AirConditioner (got %T)", did, d) }
if !ok {
return nil, fmt.Errorf("bridge: device %s is not an AirConditioner (got %T)", did, d)
}
return ac, nil
}
func (b *Bridge) Light(did string) (devices.Light, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s not found", did)
}
lt, ok := d.(devices.Light)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Light", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s is not a Light", did)
}
return lt, nil
}
func (b *Bridge) Switch(did string) (devices.Switch, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s not found", did)
}
sw, ok := d.(devices.Switch)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Switch", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s is not a Switch", did)
}
return sw, nil
}
func (b *Bridge) Fan(did string) (devices.Fan, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s not found", did)
}
f, ok := d.(devices.Fan)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Fan", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s is not a Fan", did)
}
return f, nil
}
func (b *Bridge) Cover(did string) (devices.Cover, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s not found", did)
}
cv, ok := d.(devices.Cover)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Cover", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s is not a Cover", did)
}
return cv, nil
}
func (b *Bridge) Humidifier(did string) (devices.Humidifier, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s not found", did)
}
h, ok := d.(devices.Humidifier)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Humidifier", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s is not a Humidifier", did)
}
return h, nil
}
func (b *Bridge) Vacuum(did string) (devices.Vacuum, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s not found", did)
}
v, ok := d.(devices.Vacuum)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Vacuum", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s is not a Vacuum", did)
}
return v, nil
}
func (b *Bridge) WaterHeater(did string) (devices.WaterHeater, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s not found", did)
}
wh, ok := d.(devices.WaterHeater)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a WaterHeater", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s is not a WaterHeater", did)
}
return wh, nil
}
func (b *Bridge) TempHumidity(did string) (devices.TempHumiditySensor, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s not found", did)
}
s, ok := d.(devices.TempHumiditySensor)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a TempHumiditySensor", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s is not a TempHumiditySensor", did)
}
return s, nil
}
func (b *Bridge) Occupancy(did string) (devices.OccupancySensor, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s not found", did)
}
s, ok := d.(devices.OccupancySensor)
if !ok { return nil, fmt.Errorf("bridge: device %s is not an OccupancySensor", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s is not an OccupancySensor", did)
}
return s, nil
}
func (b *Bridge) Plug(did string) (devices.Plug, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s not found", did)
}
pg, ok := d.(devices.Plug)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Plug", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s is not a Plug", did)
}
return pg, nil
}
func (b *Bridge) Speaker(did string) (devices.Speaker, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s not found", did)
}
sp, ok := d.(devices.Speaker)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Speaker", did) }
if !ok {
return nil, fmt.Errorf("bridge: device %s is not a Speaker", did)
}
return sp, nil
}
+20 -14
View File
@@ -6,6 +6,8 @@ import (
"xiaomihome/xiaomi"
"xiaomihome/xiaomi/devices"
"github.com/sirupsen/logrus"
)
// ── 回调 ──
@@ -88,7 +90,9 @@ func (b *Bridge) subscribeDevice(did string, d interface{}) {
})
}
// 订阅设备在线/离线
if bd, ok := d.(interface{ SubDeviceState(xiaomi.DeviceStateHandler) error }); ok {
if bd, ok := d.(interface {
SubDeviceState(xiaomi.DeviceStateHandler) error
}); ok {
if err := bd.SubDeviceState(func(did string, state xiaomi.DeviceState) {
online := state == xiaomi.StateOnline
b.fireOnline(did, online)
@@ -96,7 +100,7 @@ func (b *Bridge) subscribeDevice(did string, d interface{}) {
go b.client.RefreshDeviceAllProps(did)
}
}); err != nil {
b.lgr.Warnf("[bridge] %d SubDeviceState %s: %v", b.id, did, err)
logrus.Warnf("[bridge] %d SubDeviceState %s: %v", b.id, did, err)
}
}
}
@@ -120,7 +124,9 @@ func (b *Bridge) fire(did, devType, changed string, props interface{}) {
}
func deviceTypeNameRaw(d interface{}) string {
if d == nil { return "unknown" }
if d == nil {
return "unknown"
}
return deviceTypeName(d)
}
@@ -134,47 +140,47 @@ func (b *Bridge) FetchAllStates(ctx context.Context) {
switch ctrl := d.(type) {
case devices.AirConditioner:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d AC state: on=%v mode=%s target=%.1f", b.id, s.On, s.Mode, s.TargetTemperature)
logrus.Debugf("[bridge] %d AC state: on=%v mode=%s target=%.1f", b.id, s.On, s.Mode, s.TargetTemperature)
}
case devices.Light:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Light state: on=%v brightness=%d", b.id, s.On, s.Brightness)
logrus.Debugf("[bridge] %d Light state: on=%v brightness=%d", b.id, s.On, s.Brightness)
}
case devices.Switch:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Switch state: %v", b.id, s)
logrus.Debugf("[bridge] %d Switch state: %v", b.id, s)
}
case devices.Fan:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Fan state: on=%v level=%d", b.id, s.On, s.FanLevel)
logrus.Debugf("[bridge] %d Fan state: on=%v level=%d", b.id, s.On, s.FanLevel)
}
case devices.Humidifier:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Humidifier state: on=%v hum=%d", b.id, s.On, s.TargetHumidity)
logrus.Debugf("[bridge] %d Humidifier state: on=%v hum=%d", b.id, s.On, s.TargetHumidity)
}
case devices.Vacuum:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Vacuum state: on=%v mode=%d", b.id, s.On, s.Mode)
logrus.Debugf("[bridge] %d Vacuum state: on=%v mode=%d", b.id, s.On, s.Mode)
}
case devices.WaterHeater:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Heater: on=%v target=%.1f", b.id, s.On, s.TargetTemp)
logrus.Debugf("[bridge] %d Heater: on=%v target=%.1f", b.id, s.On, s.TargetTemp)
}
case devices.Cover:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Cover: pos=%d status=%d fault=%d", b.id, s.Position, s.Status, s.Fault)
logrus.Debugf("[bridge] %d Cover: pos=%d status=%d fault=%d", b.id, s.Position, s.Status, s.Fault)
}
case devices.TempHumiditySensor:
if t, h, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Sensor: temp=%.1f hum=%.1f", b.id, t, h)
logrus.Debugf("[bridge] %d Sensor: temp=%.1f hum=%.1f", b.id, t, h)
}
case devices.OccupancySensor:
if o, l, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Occupancy: occ=%v lux=%.0f", b.id, o, l)
logrus.Debugf("[bridge] %d Occupancy: occ=%v lux=%.0f", b.id, o, l)
}
case devices.Plug:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Plug state: on=%v", b.id, s.On)
logrus.Debugf("[bridge] %d Plug state: on=%v", b.id, s.On)
}
}
}