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

164 lines
12 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

- **`internal/infra/database/storage.go` (`database.Storage`)**
- **工厂函数改造 (`NewStorage`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 在函数内部,将接收到的 `ctx` 作为第一个参数传递给 `NewPostgresStorage` 函数。
- **`internal/infra/database/postgres.go`**
- **结构体改造**:
- [x] 移除 `PostgresStorage` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewPostgresStorage`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 在函数内部,为 `PostgresStorage` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "PostgresStorage")`
- [x] 将这个 `selfCtx` 赋值给 `PostgresStorage` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Connect`, `Disconnect`, `Migrate`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, ps.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `ps.logger.Info`, `ps.logger.Errorw`, `ps.logger.Debugw` 的调用替换为 `logger.Info`,
`logger.Errorw`, `logger.Debugw`
- [x] 在 `Connect` 方法中,调用 `logs.NewGormLogger` 时,将 `newCtx` 传递给它。
- [x] 在 `Connect` 方法中,调用 `gorm.Open` 时,使用
`ps.db, err = gorm.Open(postgres.Open(ps.connectionString), &gorm.Config{Logger: logs.NewGormLogger(newCtx)})`
- [x] 在 `Migrate` 方法中,确保所有对 `ps.db.AutoMigrate``ps.db.Exec` 的调用都使用 `newCtx`
- **内部辅助方法改造 (`setupTimescaleDB`, `creatingHyperTable`, `applyCompressionPolicies`, `creatingIndex`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在每个方法入口处,使用 `newCtx, logger := logs.Trace(ctx, ps.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `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`
- [x] 确保所有对 `ps.db.Exec` 的调用都使用 `newCtx`
- **`internal/infra/notify/log_notifier.go` (`notify.LogNotifier`)**
- **结构体改造**:
- [x] 移除 `logNotifier` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewLogNotifier`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 在函数内部,为 `logNotifier` 创建其专属的 `selfCtx``selfCtx := logs.AddCompName(ctx, "LogNotifier")`
- [x] 将这个 `selfCtx` 赋值给 `logNotifier` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Send`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, l.selfCtx, "Send")` 获取新的 `context.Context`
`logger` 实例。
- [x] 将所有对 `l.logger.Infow` 的调用替换为 `logger.Infow`
- **公共方法 (`Type`)**:
- [x] 此方法不涉及日志或上下文传递,无需改造。
- **`internal/infra/notify/lark.go` (`notify.LarkNotifier`)**
- **结构体改造**:
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewLarkNotifier`)**:
- [x] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在函数内部,为 `larkNotifier` 创建其专属的 `selfCtx``selfCtx := logs.AddCompName(ctx, "LarkNotifier")`
- [x] 将这个 `selfCtx` 赋值给 `larkNotifier` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Send`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, l.selfCtx, "Send")` 获取新的 `context.Context`
`logger` 实例。
- [x] 将所有内部的错误日志(例如 `fmt.Errorf` 返回的错误,以及 `getAccessToken` 中的错误)通过 `logger.Errorf`
记录。
- **内部辅助方法改造 (`getAccessToken`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, l.selfCtx, "getAccessToken")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有内部的错误日志通过 `logger.Errorf` 记录。
- **`internal/infra/notify/smtp.go` (`notify.SMTPNotifier`)**
- **结构体改造**:
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewSMTPNotifier`)**:
- [x] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在函数内部,为 `smtpNotifier` 创建其专属的 `selfCtx``selfCtx := logs.AddCompName(ctx, "SMTPNotifier")`
- [x] 将这个 `selfCtx` 赋值给 `smtpNotifier` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Send`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "Send")` 获取新的 `context.Context`
`logger` 实例。
- [x] 将所有内部的错误日志(例如 `fmt.Errorf` 返回的错误)通过 `logger.Errorf` 记录。
- **`internal/infra/notify/wechat.go` (`notify.WechatNotifier`)**
- **结构体改造**:
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewWechatNotifier`)**:
- [x] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在函数内部,为 `wechatNotifier` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "WechatNotifier")`
- [x] 将这个 `selfCtx` 赋值给 `wechatNotifier` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Send`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, w.selfCtx, "Send")` 获取新的 `context.Context`
`logger` 实例。
- [x] 将所有内部的错误日志(例如 `fmt.Errorf` 返回的错误,以及 `getAccessToken` 中的错误)通过 `logger.Errorf`
记录。
- **内部辅助方法改造 (`getAccessToken`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, w.selfCtx, "getAccessToken")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有内部的错误日志通过 `logger.Errorf` 记录。
- **`internal/infra/transport/lora/chirp_stack.go` (`lora.ChirpStackTransport`)**
- **结构体改造**:
- [x] 移除 `ChirpStackTransport` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewChirpStackTransport`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 在函数内部,为 `ChirpStackTransport` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "ChirpStackTransport")`
- [x] 将这个 `selfCtx` 赋值给 `ChirpStackTransport` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Send`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, c.selfCtx, "Send")` 获取新的 `context.Context`
`logger` 实例。
- [x] 将所有对 `c.logger.Errorf``c.logger.Infof` 的调用替换为 `logger.Errorf``logger.Infof`
- [x] 在调用 `c.client.DeviceService.DeviceServiceEnqueue` 时,确保将 `newCtx` 传递给
`params.WithContext(newCtx)`,以便 ChirpStack 客户端内部的 HTTP 请求也能携带上下文。
- **`internal/infra/transport/lora/lora_mesh_uart_passthrough_transport.go` (`lora.LoRaMeshUartPassthroughTransport`)**
- **结构体改造**:
- [x] 移除 `LoRaMeshUartPassthroughTransport` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewLoRaMeshUartPassthroughTransport`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 在函数内部,为 `LoRaMeshUartPassthroughTransport` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "LoRaMeshUartPassthroughTransport")`
- [x] 将这个 `selfCtx` 赋值给 `LoRaMeshUartPassthroughTransport` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Listen`, `Send`, `Stop`)**:
- [x] 修改 `Listen` 方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 修改 `Send` 方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 修改 `Stop` 方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, t.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `t.logger.Info`, `t.logger.Errorf` 的调用替换为 `logger.Info`, `logger.Errorf`
- [x] 在 `Send` 方法中,确保 `t.sendChan <- req` 传递的 `req` 包含了 `newCtx`
- **内部辅助方法改造 (`workerLoop`, `runIdleState`, `runReceivingState`, `executeSend`, `handleFrame`,
`handleUpstreamMessage`, `recordSensorData`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在每个方法入口处,使用 `newCtx, logger := logs.Trace(ctx, t.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `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`
- [x] 确保所有对 `t.port`, `t.areaControllerRepo`, `t.pendingCollectionRepo`, `t.deviceRepo`,
`t.sensorDataRepo` 等依赖的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/infra/transport/lora/placeholder_transport.go` (`lora.PlaceholderTransport`)**
- **结构体改造**:
- [x] 移除 `PlaceholderTransport` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewPlaceholderTransport`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 在函数内部,为 `PlaceholderTransport` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "PlaceholderTransport")`
- [x] 将这个 `selfCtx` 赋值给 `PlaceholderTransport` 结构体的 `selfCtx` 成员。
- [x] 使用 `newCtx, logger := logs.Trace(ctx, selfCtx, "NewPlaceholderTransport")` 获取 `logger` 实例,并替换
`logger.Info` 调用。
- **公共方法改造 (`Listen`, `Stop`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, p.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `p.logger.Warnf` 的调用替换为 `logger.Warnf`