Files
pig-farm-controller/design/provide-logger-with-mothed/task-infra.md
2025-11-05 22:22:46 +08:00

12 KiB
Raw Blame History

  • internal/infra/database/storage.go (database.Storage)

    • 工厂函数改造 (NewStorage):
      • 修改函数签名,移除 logger *logs.Logger 参数,改为接收 ctx context.Context
      • 在函数内部,将接收到的 ctx 作为第一个参数传递给 NewPostgresStorage 函数。
  • internal/infra/database/postgres.go

    • 结构体改造:
      • 移除 PostgresStorage 结构体中的 logger *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewPostgresStorage):
      • 修改函数签名,移除 logger *logs.Logger 参数,改为接收 ctx context.Context
      • 在函数内部,为 PostgresStorage 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "PostgresStorage")
      • 将这个 selfCtx 赋值给 PostgresStorage 结构体的 selfCtx 成员。
    • 公共方法改造 (Connect, Disconnect, Migrate):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, ps.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 ps.logger.Info, ps.logger.Errorw, ps.logger.Debugw 的调用替换为 logger.Info, logger.Errorw, logger.Debugw
      • Connect 方法中,调用 logs.NewGormLogger 时,将 newCtx 传递给它。
      • Connect 方法中,调用 gorm.Open 时,使用 ps.db, err = gorm.Open(postgres.Open(ps.connectionString), &gorm.Config{Logger: logs.NewGormLogger(newCtx)})
      • Migrate 方法中,确保所有对 ps.db.AutoMigrateps.db.Exec 的调用都使用 newCtx
    • 内部辅助方法改造 (setupTimescaleDB, creatingHyperTable, applyCompressionPolicies, creatingIndex):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在每个方法入口处,使用 newCtx, logger := logs.Trace(ctx, ps.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 ps.logger.Info, ps.logger.Errorw, ps.logger.Debugw, ps.logger.Warnw, ps.logger.Debug 的调用替换为 logger.Info, logger.Errorw, logger.Debugw, logger.Warnw, logger.Debug
      • 确保所有对 ps.db.Exec 的调用都使用 newCtx
  • internal/infra/notify/log_notifier.go (notify.LogNotifier)

    • 结构体改造:
      • 移除 logNotifier 结构体中的 logger *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewLogNotifier):
      • 修改函数签名,移除 logger *logs.Logger 参数,改为接收 ctx context.Context
      • 在函数内部,为 logNotifier 创建其专属的 selfCtxselfCtx := logs.AddCompName(ctx, "LogNotifier")
      • 将这个 selfCtx 赋值给 logNotifier 结构体的 selfCtx 成员。
    • 公共方法改造 (Send):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, l.selfCtx, "Send") 获取新的 context.Contextlogger 实例。
      • 将所有对 l.logger.Infow 的调用替换为 logger.Infow
    • 公共方法 (Type):
      • 此方法不涉及日志或上下文传递,无需改造。
  • internal/infra/notify/lark.go (notify.LarkNotifier)

    • 结构体改造:
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewLarkNotifier):
      • 修改函数签名,添加 ctx context.Context 作为第一个参数。
      • 在函数内部,为 larkNotifier 创建其专属的 selfCtxselfCtx := logs.AddCompName(ctx, "LarkNotifier")
      • 将这个 selfCtx 赋值给 larkNotifier 结构体的 selfCtx 成员。
    • 公共方法改造 (Send):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, l.selfCtx, "Send") 获取新的 context.Contextlogger 实例。
      • 将所有内部的错误日志(例如 fmt.Errorf 返回的错误,以及 getAccessToken 中的错误)通过 logger.Errorf 记录。
    • 内部辅助方法改造 (getAccessToken):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, l.selfCtx, "getAccessToken") 获取新的 context.Contextlogger 实例。
      • 将所有内部的错误日志通过 logger.Errorf 记录。
  • internal/infra/notify/smtp.go (notify.SMTPNotifier)

    • 结构体改造:
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewSMTPNotifier):
      • 修改函数签名,添加 ctx context.Context 作为第一个参数。
      • 在函数内部,为 smtpNotifier 创建其专属的 selfCtxselfCtx := logs.AddCompName(ctx, "SMTPNotifier")
      • 将这个 selfCtx 赋值给 smtpNotifier 结构体的 selfCtx 成员。
    • 公共方法改造 (Send):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, s.selfCtx, "Send") 获取新的 context.Contextlogger 实例。
      • 将所有内部的错误日志(例如 fmt.Errorf 返回的错误)通过 logger.Errorf 记录。
  • internal/infra/notify/wechat.go (notify.WechatNotifier)

    • 结构体改造:
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewWechatNotifier):
      • 修改函数签名,添加 ctx context.Context 作为第一个参数。
      • 在函数内部,为 wechatNotifier 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "WechatNotifier")
      • 将这个 selfCtx 赋值给 wechatNotifier 结构体的 selfCtx 成员。
    • 公共方法改造 (Send):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, w.selfCtx, "Send") 获取新的 context.Contextlogger 实例。
      • 将所有内部的错误日志(例如 fmt.Errorf 返回的错误,以及 getAccessToken 中的错误)通过 logger.Errorf 记录。
    • 内部辅助方法改造 (getAccessToken):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, w.selfCtx, "getAccessToken") 获取新的 context.Contextlogger 实例。
      • 将所有内部的错误日志通过 logger.Errorf 记录。
  • internal/infra/transport/lora/chirp_stack.go (lora.ChirpStackTransport)

    • 结构体改造:
      • 移除 ChirpStackTransport 结构体中的 logger *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewChirpStackTransport):
      • 修改函数签名,移除 logger *logs.Logger 参数,改为接收 ctx context.Context
      • 在函数内部,为 ChirpStackTransport 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "ChirpStackTransport")
      • 将这个 selfCtx 赋值给 ChirpStackTransport 结构体的 selfCtx 成员。
    • 公共方法改造 (Send):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, c.selfCtx, "Send") 获取新的 context.Contextlogger 实例。
      • 将所有对 c.logger.Errorfc.logger.Infof 的调用替换为 logger.Errorflogger.Infof
      • 在调用 c.client.DeviceService.DeviceServiceEnqueue 时,确保将 newCtx 传递给 params.WithContext(newCtx),以便 ChirpStack 客户端内部的 HTTP 请求也能携带上下文。
  • internal/infra/transport/lora/lora_mesh_uart_passthrough_transport.go (lora.LoRaMeshUartPassthroughTransport)

    • 结构体改造:
      • 移除 LoRaMeshUartPassthroughTransport 结构体中的 logger *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewLoRaMeshUartPassthroughTransport):
      • 修改函数签名,移除 logger *logs.Logger 参数,改为接收 ctx context.Context
      • 在函数内部,为 LoRaMeshUartPassthroughTransport 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "LoRaMeshUartPassthroughTransport")
      • 将这个 selfCtx 赋值给 LoRaMeshUartPassthroughTransport 结构体的 selfCtx 成员。
    • 公共方法改造 (Listen, Send, Stop):
      • 修改 Listen 方法签名,添加 ctx context.Context 作为第一个参数。
      • 修改 Send 方法签名,添加 ctx context.Context 作为第一个参数。
      • 修改 Stop 方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, t.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 t.logger.Info, t.logger.Errorf 的调用替换为 logger.Info, logger.Errorf
      • Send 方法中,确保 t.sendChan <- req 传递的 req 包含了 newCtx
    • 内部辅助方法改造 (workerLoop, runIdleState, runReceivingState, executeSend, handleFrame, handleUpstreamMessage, recordSensorData):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在每个方法入口处,使用 newCtx, logger := logs.Trace(ctx, t.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 t.logger.Info, t.logger.Errorf, t.logger.Warnf, t.logger.Infof, t.logger.Debugf 的调用替换为 logger.Info, logger.Errorf, logger.Warnf, logger.Infof, logger.Debugf
      • 确保所有对 t.port, t.areaControllerRepo, t.pendingCollectionRepo, t.deviceRepo, t.sensorDataRepo 等依赖的调用都将 newCtx 作为第一个参数传递。
  • internal/infra/transport/lora/placeholder_transport.go (lora.PlaceholderTransport)

    • 结构体改造:
      • 移除 PlaceholderTransport 结构体中的 logger *logs.Logger 成员。
      • 新增 selfCtx context.Context 成员。
    • 构造函数改造 (NewPlaceholderTransport):
      • 修改函数签名,移除 logger *logs.Logger 参数,改为接收 ctx context.Context
      • 在函数内部,为 PlaceholderTransport 创建其专属的 selfCtx selfCtx := logs.AddCompName(ctx, "PlaceholderTransport")
      • 将这个 selfCtx 赋值给 PlaceholderTransport 结构体的 selfCtx 成员。
      • 使用 newCtx, logger := logs.Trace(ctx, selfCtx, "NewPlaceholderTransport") 获取 logger 实例,并替换 logger.Info 调用。
    • 公共方法改造 (Listen, Stop):
      • 修改方法签名,添加 ctx context.Context 作为第一个参数。
      • 在方法入口处,使用 newCtx, logger := logs.Trace(ctx, p.selfCtx, "MethodName") 获取新的 context.Contextlogger 实例。
      • 将所有对 p.logger.Warnf 的调用替换为 logger.Warnf