From 09898ec5c2ef758f0dd05a6966b63a1de7fd9830 Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Mon, 3 Nov 2025 23:17:06 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=A6=E7=BB=86=E4=BB=BB=E5=8A=A1=E6=B8=85?= =?UTF-8?q?=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- design/provide-logger-with-mothed/task-api.md | 9 + .../task-controller.md | 135 ++++++ .../provide-logger-with-mothed/task-domain.md | 272 ++++++++++++ .../provide-logger-with-mothed/task-infra.md | 163 +++++++ .../provide-logger-with-mothed/task-list.md | 103 ++--- .../task-middleware.md | 25 ++ .../task-repository.md | 396 ++++++++++++++++++ .../task-service.md | 113 +++++ .../task-webhook.md | 39 ++ 9 files changed, 1191 insertions(+), 64 deletions(-) create mode 100644 design/provide-logger-with-mothed/task-api.md create mode 100644 design/provide-logger-with-mothed/task-controller.md create mode 100644 design/provide-logger-with-mothed/task-domain.md create mode 100644 design/provide-logger-with-mothed/task-infra.md create mode 100644 design/provide-logger-with-mothed/task-middleware.md create mode 100644 design/provide-logger-with-mothed/task-repository.md create mode 100644 design/provide-logger-with-mothed/task-service.md create mode 100644 design/provide-logger-with-mothed/task-webhook.md diff --git a/design/provide-logger-with-mothed/task-api.md b/design/provide-logger-with-mothed/task-api.md new file mode 100644 index 0000000..93a2dc0 --- /dev/null +++ b/design/provide-logger-with-mothed/task-api.md @@ -0,0 +1,9 @@ +- **`internal/app/api/api.go` (`API`)**: + - [ ] 修改 `NewAPI` 函数,移除 `logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 移除 `API` 结构体中的 `logger` 成员,改为保存 `selfCtx context.Context`。 + - [ ] 为 `API` 组件本身创建 `selfCtx`:`selfCtx := logs.AddCompName(ctx, 'API')`,并传递给所有 `Controller` + 的构造函数。 + - [ ] 改造 `Start` 方法,从 `a.selfCtx` 获取 `logger` 实例进行日志记录。 + - [ ] 改造 `Stop` 方法,从 `a.selfCtx` 获取 `logger` 实例进行日志记录。 +- **`internal/app/api/router.go` + - [ ] 改造 `setupRoutes` 方法,从 `a.selfCtx` 获取 `logger` 实例进行日志记录。 diff --git a/design/provide-logger-with-mothed/task-controller.md b/design/provide-logger-with-mothed/task-controller.md new file mode 100644 index 0000000..58a36cd --- /dev/null +++ b/design/provide-logger-with-mothed/task-controller.md @@ -0,0 +1,135 @@ +- **`internal/app/controller/user/user_controller.go` (`user.Controller`)** + - **结构体改造**: + - [ ] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewController`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `user.Controller` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "UserController")`。 + - [ ] 将这个 `selfCtx` 赋值给 `Controller` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`CreateUser`, `Login`, `SendTestNotification`)**: + - [ ] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。 + - [ ] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.selfCtx, "MethodName")` 获取新的 `context.Context` 和 + `logger` 实例。 + - [ ] 将所有对 `c.logger.Errorf` 和 `c.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。 + - [ ] 对于 `SendTestNotification` 方法中的 `controller.SendErrorWithAudit` 和 + `controller.SendSuccessWithAudit` 调用,需要将 `newCtx` 作为第一个参数传递。 + - [ ] 确保所有对 `c.userService` 的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/app/controller/device/device_controller.go` (`device.Controller`)** + - **结构体改造**: + - [ ] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewController`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `device.Controller` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "DeviceController")`。 + - [ ] 将这个 `selfCtx` 赋值给 `Controller` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`CreateDevice`, `GetDevice`, `ListDevices`, `UpdateDevice`, `DeleteDevice`, `ManualControl`, + `CreateAreaController`, `GetAreaController`, `ListAreaControllers`, `UpdateAreaController`, + `DeleteAreaController`, `CreateDeviceTemplate`, `GetDeviceTemplate`, `ListDeviceTemplates`, + `UpdateDeviceTemplate`, `DeleteDeviceTemplate`)**: + - [ ] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。 + - [ ] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.selfCtx, "MethodName")` 获取新的 `context.Context` 和 + `logger` 实例。 + - [ ] 将所有对 `c.logger.Errorf`, `c.logger.Warnf`, `c.logger.Infof` 的调用替换为 `logger.Errorf`, + `logger.Warnf`, `logger.Infof`。 + - [ ] 对于 `controller.SendErrorWithAudit` 和 `controller.SendSuccessWithAudit` 调用,需要将 `newCtx` + 作为第一个参数传递。 + - [ ] 确保所有对 `c.deviceService` 的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/app/controller/plan/plan_controller.go` (`plan.Controller`)** + - **结构体改造**: + - [ ] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewController`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `plan.Controller` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PlanController")`。 + - [ ] 将这个 `selfCtx` 赋值给 `Controller` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`CreatePlan`, `GetPlan`, `ListPlans`, `UpdatePlan`, `DeletePlan`, `StartPlan`, `StopPlan`)**: + - [ ] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。 + - [ ] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.selfCtx, "MethodName")` 获取新的 `context.Context` 和 + `logger` 实例。 + - [ ] 将所有对 `c.logger.Errorf` 和 `c.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。 + - [ ] 对于 `controller.SendErrorWithAudit` 和 `controller.SendSuccessWithAudit` 调用,需要将 `newCtx` + 作为第一个参数传递。 + - [ ] 确保所有对 `c.planService` 的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/app/controller/management/pig_farm_controller.go` (`management.PigFarmController`)** + - **结构体改造**: + - [ ] 移除 `PigFarmController` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewPigFarmController`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `PigFarmController` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigFarmController")`。 + - [ ] 将这个 `selfCtx` 赋值给 `PigFarmController` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`CreatePigHouse`, `GetPigHouse`, `ListPigHouses`, `UpdatePigHouse`, `DeletePigHouse`, `CreatePen`, + `GetPen`, `ListPens`, `UpdatePen`, `DeletePen`, `UpdatePenStatus`)**: + - [ ] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。 + - [ ] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.selfCtx, "MethodName")` 获取新的 `context.Context` 和 + `logger` 实例。 + - [ ] 将所有对 `c.logger.Errorf` 和 `c.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。 + - [ ] 对于 `controller.SendErrorWithAudit` 和 `controller.SendSuccessWithAudit` 调用,需要将 `newCtx` + 作为第一个参数传递。 + - [ ] 确保所有对 `c.service` 的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/app/controller/management/pig_batch_controller.go` (`management.PigBatchController`)** + - **结构体改造**: + - [ ] 移除 `PigBatchController` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewPigBatchController`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `PigBatchController` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigBatchController")`。 + - [ ] 将这个 `selfCtx` 赋值给 `PigBatchController` 结构体的 `selfCtx` 成员。 + - **公共方法改造 ( + 所有方法,例如 `CreatePigBatch`, `GetPigBatch`, `UpdatePigBatch`, `DeletePigBatch`, `ListPigBatches`, + `AssignEmptyPensToBatch`, `ReclassifyPenToNewBatch`, `RemoveEmptyPenFromBatch`, `MovePigsIntoPen`)**: + - [ ] 这些方法通过调用 `controller_helpers.go` 中的 `handleAPIRequest...` 系列函数来处理请求。 + - [ ] 确保传递给 `handleAPIRequest...` 系列函数的 `serviceExecutor` 匿名函数,其签名与 `controller_helpers.go` + 改造后期望的签名一致(即接收 `context.Context` 作为第一个参数)。 + - [ ] 在 `serviceExecutor` 匿名函数内部,将接收到的 `context.Context` 传递给 `c.service` 的相应方法。 +- **`internal/app/controller/management/controller_helpers.go`** + - [ ] 修改 `mapAndSendError` 函数中的 `c.logger.Errorf` 调用,改为从 `echo.Context` 中提取 `context.Context`,并使用 + `logs.Trace` 或 `logs.GetLogger` 获取 `logger` 实例进行日志记录。 + - [ ] 检查其他函数是否直接或间接依赖 `*PigBatchController` 的 `logger` 成员,并进行相应改造。 +- **`internal/app/controller/management/pig_batch_trade_controller.go` (`management.PigBatchController`)** + - [ ] 修改 `NewController` 函数,移除 `logger` 参数,改为接收 `selfCtx context.Context`。 + - [ ] 移除 `Controller` 结构体中的 `logger` 成员,改为保存 `selfCtx`。 + - [ ] **所有公共方法**: 接收 `echo.Context` 的方法,需要从中提取 `request.Context()` 作为 `upstreamCtx`。 +- **`internal/app/controller/management/pig_batch_health_controller.go` (`management.PigBatchController`)** + - [ ] 修改 `NewController` 函数,移除 `logger` 参数,改为接收 `selfCtx context.Context`。 + - [ ] 移除 `Controller` 结构体中的 `logger` 成员,改为保存 `selfCtx`。 + - [ ] **所有公共方法**: 接收 `echo.Context` 的方法,需要从中提取 `request.Context()` 作为 `upstreamCtx`。 +- **`internal/app/controller/management/pig_batch_transfer_controller.go` (`management.PigBatchController`)** + - [ ] 修改 `NewController` 函数,移除 `logger` 参数,改为接收 `selfCtx context.Context`。 + - [ ] 移除 `Controller` 结构体中的 `logger` 成员,改为保存 `selfCtx`。 + - [ ] **所有公共方法**: 接收 `echo.Context` 的方法,需要从中提取 `request.Context()` 作为 `upstreamCtx`。 +- **`internal/app/controller/monitor/monitor_controller.go` (`monitor.Controller`)** + - **结构体改造**: + - [ ] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewController`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `monitor.Controller` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "MonitorController")`。 + - [ ] 将这个 `selfCtx` 赋值给 `Controller` 结构体的 `selfCtx` 成员。 + - **公共方法改造 ( + 所有方法,例如 `ListSensorData`, `ListDeviceCommandLogs`, `ListPlanExecutionLogs`, `ListTaskExecutionLogs`, + `ListPendingCollections`, `ListUserActionLogs`, `ListRawMaterialPurchases`, `ListRawMaterialStockLogs`, + `ListFeedUsageRecords`, `ListMedicationLogs`, `ListPigBatchLogs`, `ListWeighingBatches`, `ListWeighingRecords`, + `ListPigTransferLogs`, `ListPigSickLogs`, `ListPigPurchases`, `ListPigSales`, `ListNotifications`)**: + - [ ] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`。 + - [ ] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.selfCtx, actionType)` 获取新的 `context.Context` 和 + `logger` 实例(`actionType` 为方法内部定义的常量)。 + - [ ] 将所有对 `c.logger.Errorf`, `c.logger.Warnf`, `c.logger.Infof` 的调用替换为 `logger.Errorf`, + `logger.Warnf`, `logger.Infof`。 + - [ ] 对于 `controller.SendErrorWithAudit` 和 `controller.SendSuccessWithAudit` 调用,需要将 `newCtx` + 作为第一个参数传递。 + - [ ] 确保所有对 `c.monitorService` 的调用都将 `newCtx` 作为第一个参数传递。 + + +- **`internal/app/controller/response.go`** + - [ ] 修改 `SendSuccessWithAudit` 和 `SendErrorWithAudit` 函数签名,使其接收 `ctx context.Context` 作为第一个参数。 + - [ ] 在 `setAuditDetails` 函数中,如果需要,从传入的 `context.Context` 中提取调用链信息,并将其添加到审计信息中。 +- **`internal/app/controller/auth_utils.go`** + - [ ] 检查 `GetOperatorIDFromContext` 和 `GetOperatorFromContext` 函数,确保它们能够正确地从 `echo.Context` 中获取 + `request.Context()`,并从中提取用户信息。 \ No newline at end of file diff --git a/design/provide-logger-with-mothed/task-domain.md b/design/provide-logger-with-mothed/task-domain.md new file mode 100644 index 0000000..25becb2 --- /dev/null +++ b/design/provide-logger-with-mothed/task-domain.md @@ -0,0 +1,272 @@ +- **`internal/domain/audit/service.go` (`audit.Service`)** + - **结构体改造**: + - [ ] 移除 `service` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewService`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `audit.Service` 创建其专属的 `selfCtx`:`selfCtx := logs.AddCompName(ctx, "AuditService")`。 + - [ ] 将这个 `selfCtx` 赋值给 `service` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`LogAction`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "LogAction")` 获取新的 `context.Context` + 和 `logger` 实例。 + - [ ] 将所有对 `s.logger.Warnw` 和 `s.logger.Errorw` 的调用替换为 `logger.Warnw` 和 `logger.Errorw`。 + - [ ] 确保所有对 `s.userActionLogRepository.Create` 的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/domain/device/general_device_service.go` (`device.Service`)** + - **结构体改造**: + - [ ] 移除 `GeneralDeviceService` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGeneralDeviceService`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `GeneralDeviceService` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "GeneralDeviceService")`。 + - [ ] 将这个 `selfCtx` 赋值给 `GeneralDeviceService` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`Switch`, `Collect`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, g.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `g.logger.Errorf`, `g.logger.Infof`, `g.logger.Warnf`, `g.logger.Debugf`, `g.logger.DPanicf` + 的调用替换为 `logger.Errorf`, `logger.Infof`, `logger.Warnf`, `logger.Debugf`, `logger.DPanicf`。 + - [ ] 确保所有对 `g.deviceRepo`, `g.deviceCommandLogRepo`, `g.pendingCollectionRepo`, `g.comm` 等依赖的调用都将 + `newCtx` 作为第一个参数传递。 +- **`internal/domain/notify/notify.go` (`domain_notify.Service` - `failoverService` 实现)** + - **结构体改造**: + - [ ] 移除 `failoverService` 结构体中的 `log *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewFailoverService`)**: + - [ ] 修改函数签名,移除 `log *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `failoverService` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "FailoverService")`。 + - [ ] 将这个 `selfCtx` 赋值给 `failoverService` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`SendBatchAlarm`, `BroadcastAlarm`, `SendTestMessage`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `s.log.Infow`, `s.log.Errorw`, `s.log.Warnw` 的调用替换为 `logger.Infow`, `logger.Errorw`, + `logger.Warnw`。 + - [ ] 确保所有对 `s.userRepo`, `s.primaryNotifier.Send`, `s.notifiers`, `s.notificationRepo` 等依赖的调用都将 + `newCtx` 作为第一个参数传递。 + - **内部辅助方法改造 (`sendAlarmToUser`, `recordNotificationAttempt`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `s.log.Errorw`, `s.log.Infow`, `s.log.Warnw` 的调用替换为 `logger.Errorw`, `logger.Infow`, + `logger.Warnw`。 + - [ ] 确保所有对 `s.userRepo`, `s.primaryNotifier.Send`, `s.notifiers`, `s.notificationRepo` 等依赖的调用都将 + `newCtx` 作为第一个参数传递。 +- **`internal/domain/pig/pen_transfer_manager.go` (`pig.PigPenTransferManager`)** + - **结构体改造**: + - [ ] 移除 `pigPenTransferManager` 结构体中可能存在的 `logger *logs.Logger` 成员(如果未来添加)。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewPigPenTransferManager`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `pigPenTransferManager` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigPenTransferManager")`。 + - [ ] 将这个 `selfCtx` 赋值给 `pigPenTransferManager` 结构体的 `selfCtx` 成员。 + - **公共方法改造 ( + 所有方法,例如 `LogTransfer`, `GetPenByID`, `GetPensByBatchID`, `UpdatePenFields`, `GetCurrentPigsInPen`, + `GetTotalPigsInPensForBatchTx`, `ReleasePen`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数(在 `tx *gorm.DB` 之前)。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `s.logger.Errorf` 和 `s.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。 + - [ ] 确保所有对 `s.penRepo`, `s.logRepo`, `s.pigBatchRepo` 等依赖的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/domain/pig/pig_trade_manager.go` (`pig.PigTradeManager`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewPigTradeManager`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `pigTradeManager` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigTradeManager")`。 + - [ ] 将这个 `selfCtx` 赋值给 `pigTradeManager` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`SellPig`, `BuyPig`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数(在 `tx *gorm.DB` 之前)。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 确保所有对 `s.tradeRepo` 的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/domain/pig/pig_sick_manager.go` (`pig.SickPigManager`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewSickPigManager`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `sickPigManager` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "SickPigManager")`。 + - [ ] 将这个 `selfCtx` 赋值给 `sickPigManager` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`ProcessSickPigLog`, `GetCurrentSickPigCount`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数(在 `tx *gorm.DB` 之前)。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 确保所有对 `s.sickLogRepo`, `s.medicationLogRepo` 等依赖的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/domain/pig/pig_batch_service.go` (`pig.PigBatchService`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewPigBatchService`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `pigBatchService` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigBatchService")`。 + - [ ] 将这个 `selfCtx` 赋值给 `pigBatchService` 结构体的 `selfCtx` 成员。 + - **公共方法改造 ( + 所有方法,例如 `CreatePigBatch`, `GetPigBatch`, `UpdatePigBatch`, `DeletePigBatch`, `ListPigBatches`, + `AssignEmptyPensToBatch`, `MovePigsIntoPen`, `ReclassifyPenToNewBatch`, `RemoveEmptyPenFromBatch`, + `GetCurrentPigQuantity`, `GetCurrentPigsInPen`, `GetTotalPigsInPensForBatch`, `UpdatePigBatchQuantity`, + `SellPigs`, `BuyPigs`, `TransferPigsAcrossBatches`, `TransferPigsWithinBatch`, `RecordSickPigs`, + `RecordSickPigRecovery`, `RecordSickPigDeath`, `RecordSickPigCull`, `RecordDeath`, `RecordCull`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 确保所有对 `s.pigBatchRepo`, `s.pigBatchLogRepo`, `s.uow`, `s.transferSvc`, `s.tradeSvc`, `s.sickSvc` + 等依赖的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/domain/plan/analysis_plan_task_manager.go` (`plan.AnalysisPlanTaskManager`)** + - **结构体改造**: + - [ ] 移除 `analysisPlanTaskManagerImpl` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewAnalysisPlanTaskManager`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `analysisPlanTaskManagerImpl` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "AnalysisPlanTaskManager")`。 + - [ ] 将这个 `selfCtx` 赋值给 `analysisPlanTaskManagerImpl` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`Refresh`, `CreateOrUpdateTrigger`, `EnsureAnalysisTaskDefinition`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, m.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `m.logger.Info`, `m.logger.Errorf`, `m.logger.Warnf` 的调用替换为 `logger.Info`, + `logger.Errorf`, `logger.Warnf`。 + - [ ] 确保所有对 `m.planRepo`, `m.pendingTaskRepo`, `m.executionLogRepo` 等依赖的调用都将 `newCtx` 作为第一个参数传递。 + - **内部辅助方法改造 (`getRefreshData`, `cleanupInvalidTasks`, `addOrUpdateTriggers`, `createTriggerTask`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, m.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `m.logger.Info`, `m.logger.Errorf`, `m.logger.Warnf` 的调用替换为 `logger.Info`, + `logger.Errorf`, `logger.Warnf`。 + - [ ] 确保所有对 `m.planRepo`, `m.pendingTaskRepo`, `m.executionLogRepo` 等依赖的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/domain/plan/plan_execution_manager.go` (`plan.ExecutionManager`)** + - **结构体改造**: + - [ ] 移除 `planExecutionManagerImpl` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewPlanExecutionManager`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `planExecutionManagerImpl` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PlanExecutionManager")`。 + - [ ] 将这个 `selfCtx` 赋值给 `planExecutionManagerImpl` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`Start`, `Stop`)**: + - [ ] 在 `Start` 方法中,使用 `newCtx, logger := logs.Trace(context.Background(), s.selfCtx, "Start")` 获取 + `logger` 实例进行日志记录。 + - [ ] 在 `Stop` 方法中,使用 `newCtx, logger := logs.Trace(context.Background(), s.selfCtx, "Stop")` 获取 + `logger` 实例进行日志记录。 + - **内部辅助方法改造 (`run`, `claimAndSubmit`, `handleRequeue`, `processTask`, `runTask`, `analysisPlan`, + `updateTaskExecutionLogStatus`, `handlePlanTermination`, `handlePlanCompletion`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数(如果方法内部需要传递上下文)。 + - [ ] 在每个方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `s.logger.Errorf`, `s.logger.Warnf`, `s.logger.Infof`, `s.logger.DPanicf` 的调用替换为 + `logger.Errorf`, `logger.Warnf`, `logger.Infof`, `logger.DPanicf`。 + - [ ] 确保所有对 `s.pendingTaskRepo`, `s.executionLogRepo`, `s.deviceRepo`, `s.sensorDataRepo`, `s.planRepo`, + `s.analysisPlanTaskManager`, `s.taskFactory`, `s.deviceService` 等依赖的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/domain/plan/plan_service.go` (`plan.Service`)** + - **结构体改造**: + - [ ] 移除 `planServiceImpl` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewPlanService`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `planServiceImpl` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PlanService")`。 + - [ ] 将这个 `selfCtx` 赋值给 `planServiceImpl` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`Start`, `Stop`, `RefreshPlanTriggers`, `CreatePlan`, `GetPlanByID`, `ListPlans`, `UpdatePlan`, + `DeletePlan`, `StartPlan`, `StopPlan`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `s.logger.Errorf`, `s.logger.Infof`, `s.logger.Warnf` 的调用替换为 `logger.Errorf`, + `logger.Infof`, `logger.Warnf`。 + - [ ] 确保所有对 `s.executionManager`, `s.taskManager`, `s.planRepo`, `s.deviceRepo`, `s.unitOfWork`, + `s.taskFactory` 等依赖的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/domain/task/task.go` (`task.TaskFactory`)** + - **结构体改造**: + - [ ] 移除 `taskFactory` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewTaskFactory`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `taskFactory` 创建其专属的 `selfCtx`:`selfCtx := logs.AddCompName(ctx, "TaskFactory")`。 + - [ ] 将这个 `selfCtx` 赋值给 `taskFactory` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`Production`, `CreateTaskFromModel`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法内部,使用 `newCtx, logger := logs.Trace(ctx, t.selfCtx, "MethodName")` 获取新的 `context.Context` + 和 `logger` 实例。 + - [ ] 将所有对 `t.logger.Panicf` 的调用替换为 `logger.Panicf`。 + - [ ] 将所有对 `NewDelayTask`, `NewReleaseFeedWeightTask`, `NewFullCollectionTask` 的调用,传递 `newCtx`。 +- **`internal/domain/task/release_feed_weight_task.go`** + - **结构体改造**: + - [ ] 移除 `ReleaseFeedWeightTask` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewReleaseFeedWeightTask`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `ReleaseFeedWeightTask` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "ReleaseFeedWeightTask")`。 + - [ ] 将这个 `selfCtx` 赋值给 `ReleaseFeedWeightTask` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`Execute`, `OnFailure`, `ResolveDeviceIDs`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, r.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `r.logger.Infof`, `r.logger.Errorf`, `r.logger.Warnf`, `r.logger.Debugf` 的调用替换为 + `logger.Infof`, `logger.Errorf`, `logger.Warnf`, `logger.Debugf`。 + - [ ] 确保所有对 `r.deviceRepo`, `r.sensorDataRepo`, `r.feedPort` 等依赖的调用都将 `newCtx` 作为第一个参数传递。 + - **内部辅助方法改造 (`getNowWeight`, `parseParameters`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, r.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `r.logger.Errorf`, `r.logger.Warnf` 的调用替换为 `logger.Errorf`, `logger.Warnf`。 + - [ ] 确保所有对 `r.sensorDataRepo`, `r.deviceRepo` 等依赖的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/domain/task/full_collection_task.go`** + - **结构体改造**: + - [ ] 移除 `FullCollectionTask` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewFullCollectionTask`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `FullCollectionTask` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "FullCollectionTask")`。 + - [ ] 将这个 `selfCtx` 赋值给 `FullCollectionTask` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`Execute`, `OnFailure`, `ResolveDeviceIDs`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, t.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `t.logger.Infow`, `t.logger.Errorw` 的调用替换为 `logger.Infow`, `logger.Errorw`。 + - [ ] 确保所有对 `t.deviceRepo`, `t.deviceService` 等依赖的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/domain/task/delay_task.go`** + - **结构体改造**: + - [ ] 移除 `DelayTask` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewDelayTask`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `DelayTask` 创建其专属的 `selfCtx`:`selfCtx := logs.AddCompName(ctx, "DelayTask")`。 + - [ ] 将这个 `selfCtx` 赋值给 `DelayTask` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`Execute`, `OnFailure`, `ResolveDeviceIDs`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, d.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `d.logger.Infof` 和 `d.logger.Errorf` 的调用替换为 `logger.Infof` 和 `logger.Errorf`。 + - **内部辅助方法改造 (`parseParameters`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, d.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `d.logger.Errorf` 的调用替换为 `logger.Errorf`。 +- **`internal/domain/token/token_service.go` (`token.Service`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewTokenService`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `tokenService` 创建其专属的 `selfCtx`:`selfCtx := logs.AddCompName(ctx, "TokenService")`。 + - [ ] 将这个 `selfCtx` 赋值给 `tokenService` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`GenerateToken`, `ParseToken`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `s.logger.*` 的调用替换为 `logger.*` (如果存在)。 + + + + + + + + + diff --git a/design/provide-logger-with-mothed/task-infra.md b/design/provide-logger-with-mothed/task-infra.md new file mode 100644 index 0000000..c972fa5 --- /dev/null +++ b/design/provide-logger-with-mothed/task-infra.md @@ -0,0 +1,163 @@ +- **`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.Context` 和 `logger` 实例。 + - [ ] 将所有对 `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.AutoMigrate` 和 `ps.db.Exec` 的调用都使用 `newCtx`。 + - **内部辅助方法改造 (`setupTimescaleDB`, `creatingHyperTable`, `applyCompressionPolicies`, `creatingIndex`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在每个方法入口处,使用 `newCtx, logger := logs.Trace(ctx, ps.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `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` 创建其专属的 `selfCtx`:`selfCtx := logs.AddCompName(ctx, "LogNotifier")`。 + - [ ] 将这个 `selfCtx` 赋值给 `logNotifier` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`Send`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, l.selfCtx, "Send")` 获取新的 `context.Context` 和 + `logger` 实例。 + - [ ] 将所有对 `l.logger.Infow` 的调用替换为 `logger.Infow`。 + - **公共方法 (`Type`)**: + - [ ] 此方法不涉及日志或上下文传递,无需改造。 +- **`internal/infra/notify/lark.go` (`notify.LarkNotifier`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewLarkNotifier`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `larkNotifier` 创建其专属的 `selfCtx`:`selfCtx := logs.AddCompName(ctx, "LarkNotifier")`。 + - [ ] 将这个 `selfCtx` 赋值给 `larkNotifier` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`Send`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, l.selfCtx, "Send")` 获取新的 `context.Context` 和 + `logger` 实例。 + - [ ] 将所有内部的错误日志(例如 `fmt.Errorf` 返回的错误,以及 `getAccessToken` 中的错误)通过 `logger.Errorf` + 记录。 + - **内部辅助方法改造 (`getAccessToken`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, l.selfCtx, "getAccessToken")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有内部的错误日志通过 `logger.Errorf` 记录。 +- **`internal/infra/notify/smtp.go` (`notify.SMTPNotifier`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewSMTPNotifier`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `smtpNotifier` 创建其专属的 `selfCtx`:`selfCtx := logs.AddCompName(ctx, "SMTPNotifier")`。 + - [ ] 将这个 `selfCtx` 赋值给 `smtpNotifier` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`Send`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "Send")` 获取新的 `context.Context` 和 + `logger` 实例。 + - [ ] 将所有内部的错误日志(例如 `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.Context` 和 + `logger` 实例。 + - [ ] 将所有内部的错误日志(例如 `fmt.Errorf` 返回的错误,以及 `getAccessToken` 中的错误)通过 `logger.Errorf` + 记录。 + - **内部辅助方法改造 (`getAccessToken`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, w.selfCtx, "getAccessToken")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有内部的错误日志通过 `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.Context` 和 + `logger` 实例。 + - [ ] 将所有对 `c.logger.Errorf` 和 `c.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.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.Context` 和 `logger` 实例。 + - [ ] 将所有对 `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.Context` 和 `logger` 实例。 + - [ ] 将所有对 `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.Context` 和 `logger` 实例。 + - [ ] 将所有对 `p.logger.Warnf` 的调用替换为 `logger.Warnf`。 + + + + + + + + + diff --git a/design/provide-logger-with-mothed/task-list.md b/design/provide-logger-with-mothed/task-list.md index 0c274a1..6e1acd6 100644 --- a/design/provide-logger-with-mothed/task-list.md +++ b/design/provide-logger-with-mothed/task-list.md @@ -4,26 +4,42 @@ #### 1. 核心改造 (`logs` 包) -- **`internal/infra/logs/logs.go`**: - - [ ] 实现 `AddCompName`, `AddFuncName`, `GetLogger`, `Trace` 四个核心包级函数。 - - [ ] 移除旧 `Logger` 结构体中的状态字段(如 `component`)。 +- **`internal/infra/logs/logs.go` (`logs.Logger` - 内部实现,非注入)** + - **核心包级函数实现 (根据 `implementation.md` 描述)**: + - [ ] 实现 `AddCompName(ctx context.Context, compName string) context.Context`。 + - [ ] 实现 + `AddFuncName(upstreamCtx context.Context, selfCtx context.Context, funcName string) context.Context`。 + - [ ] 实现 `GetLogger(ctx context.Context) *Logger`。 + - [ ] 实现 + `Trace(upstreamCtx context.Context, selfCtx context.Context, funcName string) (context.Context, *Logger)`。 + - **`Logger` 结构体改造**: + - [ ] 无 + - **`GormLogger` 改造**: + - [ ] 修改 `GormLogger.Info` 方法,从传入的 `ctx` 中获取 `logger` 实例,并使用该实例进行日志记录。 + - [ ] 修改 `GormLogger.Warn` 方法,从传入的 `ctx` 中获取 `logger` 实例,并使用该实例进行日志记录。 + - [ ] 修改 `GormLogger.Error` 方法,从传入的 `ctx` 中获取 `logger` 实例,并使用该实例进行日志记录。 + - [ ] 修改 `GormLogger.Trace` 方法,从传入的 `ctx` 中获取 `logger` 实例,并使用该实例进行日志记录。特别是 + `With(fields...)` 的调用需要调整。 --- #### 2. 依赖注入与结构体改造 -- **`internal/core/application.go`**: +- **`internal/core/application.go`**: - [ ] 移除 `Application` 结构体中的 `Logger *logs.Logger` 成员。 - [ ] 修改 `NewApplication` 函数,使其不再创建 `logger`,而是创建根 `context.Background()`。 - - [ ] 调整 `NewApplication`,将根 `context` 传递给 `initInfrastructure`, `initDomainServices`, `initAppServices` 和 `api.NewAPI`。 + - [ ] 调整 `NewApplication`,将根 `context` 传递给 `initInfrastructure`, `initDomainServices`, `initAppServices` 和 + `api.NewAPI`。 - [ ] 移除 `Application` 结构体中所有对 `app.Logger` 的直接调用,改为通过 `context` 获取 `Logger`。 -- **`internal/core/component_initializers.go`**: - - [ ] **修改所有组件结构体定义**: 遍历所有相关组件(Controllers, Services, Repositories 等),将其结构体中的 `logger *logs.Logger` 成员变量替换为 `selfCtx context.Context`。 +- **`internal/core/component_initializers.go`**: + - [ ] **修改所有组件结构体定义**: 遍历所有相关组件(Controllers, Services, Repositories 等),将其结构体中的 + `logger *logs.Logger` 成员变量替换为 `selfCtx context.Context`。 - [ ] **重构所有 `init...` 函数**: - - 移除所有 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 - - 在每个 `init...` 函数内部,为即将创建的组件生成其专属的 `selfCtx`。例如:`selfCtx := logs.AddCompName(ctx, "ComponentName")`。 - - 将这个 `selfCtx` 注入到组件的构造函数中,并由组件保存为 `selfCtx` 成员。 + - 移除所有 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - 在每个 `init...` 函数内部,为即将创建的组件生成其专属的 `selfCtx`。例如: + `selfCtx := logs.AddCompName(ctx, 'ComponentName')`。 + - 将这个 `selfCtx` 注入到组件的构造函数中,并由组件保存为 `selfCtx` 成员。 --- @@ -33,64 +49,23 @@ ##### 3.1. API 层 -- **`internal/app/api/api.go` (`API`)**: - - [ ] 修改 `NewAPI` 函数,移除 `logger` 参数,改为接收 `ctx context.Context`。 - - [ ] 移除 `API` 结构体中的 `logger` 成员,改为保存 `selfCtx context.Context`。 - - [ ] 为 `API` 组件本身创建 `selfCtx`:`selfCtx := logs.AddCompName(ctx, "API")`,并传递给所有 `Controller` 的构造函数。 - -- **`internal/app/controller/user/user_controller.go` (`user.Controller`)** -- **`internal/app/controller/device/device_controller.go` (`device.Controller`)** -- **`internal/app/controller/plan/plan_controller.go` (`plan.Controller`)** -- **`internal/app/controller/management/pig_farm_controller.go` (`management.PigFarmController`)** -- **`internal/app/controller/management/pig_batch_controller.go` (`management.PigBatchController`)** -- **`internal/app/controller/monitor/monitor_controller.go` (`monitor.Controller`)** - - **改造动作**: - - [ ] 修改 `NewController` 函数,移除 `logger` 参数,改为接收 `selfCtx context.Context`。 - - [ ] 移除 `Controller` 结构体中的 `logger` 成员,改为保存 `selfCtx`。 - - [ ] **所有公共方法**: 接收 `echo.Context` 的方法,需要从中提取 `request.Context()` 作为 `upstreamCtx`。 - -- **`internal/app/middleware/auth.go`** -- **`internal/app/middleware/audit.go`** - - **改造动作**: - - [ ] 检查并重构所有日志记录中间件。 - - [ ] 中间件应该从请求的 `c.Request().Context()` 中提取 `upstreamCtx`。 - - [ ] 使用 `logs.Trace` 或 `logs.AddFuncName` 创建新的 `Context` 和 `Logger`。 - - [ ] **关键**: 使用 `c.SetRequest(c.Request().WithContext(newCtx))` 将更新后的 `Context` 写回 `echo.Context`,以便传递给后续的中间件和 `Controller`。 +[api](./task-api.md) +[controller](./task-controller.md) +[middleware](./task-middleware.md) +[webhook](./task-webhook.md) ##### 3.2. 应用层 (Application Services) -- **`internal/app/service/pig_farm_service.go` (`service.PigFarmService`)** -- **`internal/app/service/pig_batch_service.go` (`service.PigBatchService`)** -- **`internal/app/service/monitor_service.go` (`service.MonitorService`)** -- **`internal/app/service/device_service.go` (`service.DeviceService`)** -- **`internal/app/service/plan_service.go` (`service.PlanService`)** -- **`internal/app/service/user_service.go` (`service.UserService`)** +[service](./task-service.md) ##### 3.3. 领域层 (Domain Services) -- **`internal/domain/audit/service.go` (`audit.Service`)** -- **`internal/domain/device/general_device_service.go` (`device.Service`)** -- **`internal/domain/notify/notify.go` (`domain_notify.Service` - `failoverService` 实现)** -- **`internal/domain/pig/pen_transfer_manager.go` (`pig.PigPenTransferManager`)** -- **`internal/domain/pig/pig_trade_manager.go` (`pig.PigTradeManager`)** -- **`internal/domain/pig/pig_sick_manager.go` (`pig.SickPigManager`)** -- **`internal/domain/pig/pig_batch_service.go` (`pig.PigBatchService`)** -- **`internal/domain/plan/analysis_plan_task_manager.go` (`plan.AnalysisPlanTaskManager`)** -- **`internal/domain/plan/plan_execution_manager.go` (`plan.ExecutionManager`)** -- **`internal/domain/plan/plan_service.go` (`plan.Service`)** -- **`internal/domain/task/task.go` (`task.TaskFactory`)** +[domain](./task-domain.md) ##### 3.4. 基础设施层 (Infrastructure) -- **`internal/infra/database/storage.go` (`database.Storage`)** -- **`internal/infra/logs/logs.go` (`logs.Logger` - 内部实现,非注入)** -- **`internal/infra/notify/log_notifier.go` (`notify.LogNotifier`)** -- **`internal/infra/repository/unit_of_work.go` (`repository.UnitOfWork` - `gormUnitOfWork` 实现)** -- **`internal/infra/transport/lora/chirp_stack.go` (`lora.ChirpStackTransport`)** -- **`internal/infra/transport/lora/lora_mesh_uart_passthrough_transport.go` (`lora.LoRaMeshUartPassthroughTransport`)** -- **`internal/infra/transport/lora/placeholder_transport.go` (`lora.PlaceholderTransport`)** -- **`internal/app/webhook/chirp_stack.go` (`webhook.ChirpStackListener`)** -- **`internal/app/webhook/placeholder_listener.go` (`webhook.PlaceholderListener`)** +[repository](./task-repository.md) +[infra-other](./task-infra.md) --- @@ -99,9 +74,9 @@ - [ ] **遍历所有业务方法** (针对上述所有列出的组件的公共方法): - [ ] **定位旧日志**: 搜索所有对旧 `z.logger.*` 成员的调用。 - [ ] **改造方法入口** (对于非 Controller 方法): - 1. 在方法开始处,使用作为参数传入的 `ctx` (作为 `upstreamCtx`) 和组件自身持有的 `z.selfCtx`,调用 `logs.Trace`。 - - `newCtx, logger := logs.Trace(ctx, z.selfCtx, "MethodName")` - 2. 将所有旧的 `z.logger.*(...)` 调用,替换为使用新获取的 `logger.*(...)`。 + 1. 在方法开始处,使用作为参数传入的 `ctx` (作为 `upstreamCtx`) 和组件自身持有的 `z.selfCtx`,调用 `logs.Trace`。 + - `newCtx, logger := logs.Trace(ctx, z.selfCtx, 'MethodName')` + 2. 将所有旧的 `z.logger.*(...)` 调用,替换为使用新获取的 `logger.*(...)`。 - [ ] **改造下游调用**: - 1. 在方法内部,当需要调用其他组件的方法时(如下游服务),**必须传递 `newCtx`**。 - - `err := z.downstreamService.DoSomething(newCtx, data)` + 1. 在方法内部,当需要调用其他组件的方法时(如下游服务),**必须传递 `newCtx`**。 + - `err := z.downstreamService.DoSomething(newCtx, data)` diff --git a/design/provide-logger-with-mothed/task-middleware.md b/design/provide-logger-with-mothed/task-middleware.md new file mode 100644 index 0000000..63b270a --- /dev/null +++ b/design/provide-logger-with-mothed/task-middleware.md @@ -0,0 +1,25 @@ +- **`internal/app/middleware/auth.go`** + - **中间件函数改造 (`AuthMiddleware`)**: + - [ ] 在 `AuthMiddleware` 返回的 `echo.HandlerFunc` 内部,获取 `echo.Context` 的 `request.Context()` 作为 + `upstreamCtx`。 + - [ ] 使用 `newCtx, logger := logs.Trace(upstreamCtx, context.Background(), "AuthMiddleware")` 来创建 `newCtx` + 和 `logger` 实例。 + - [ ] 使用 `c.SetRequest(c.Request().WithContext(newCtx))` 将更新后的 `newCtx` 写入 `echo.Context`,以便后续处理链使用。 + - [ ] 将所有对 `controller.SendErrorWithStatus` 的调用替换为 `controller.SendErrorWithAudit`。 + - [ ] 确保 `controller.SendErrorWithAudit` 接收 `newCtx` 作为第一个参数,并提供适当的 `actionType`, + `description`, `targetResource`。 + - 例如,对于“请求未包含授权标头”的错误,`actionType` 可以是“认证失败”,`description` 是“请求未包含授权标头”, + `targetResource` 为 `nil`。 + - 对于“无效的Token”错误,`actionType` 可以是“认证失败”,`description` 是“无效的Token”,`targetResource` 是 + `tokenString`。 + - [ ] 在 `AuthMiddleware` 内部,如果需要日志记录(例如 `tokenService.ParseToken` 失败或 `userRepo.FindByID` + 失败),使用新创建的 `logger` 实例进行日志输出。 + - [ ] **关键**: 使用 `c.SetRequest(c.Request().WithContext(newCtx))` 将更新后的 `Context` 写回 `echo.Context` + ,以便传递给后续的中间件和 `Controller`。 +- **`internal/app/middleware/audit.go`** + - **改造动作**: + - [ ] 检查并重构所有日志记录中间件。 + - [ ] 中间件应该从请求的 `c.Request().Context()` 中提取 `upstreamCtx`。 + - [ ] 使用 `logs.Trace` 或 `logs.AddFuncName` 创建新的 `Context` 和 `Logger`。 + - [ ] **关键**: 使用 `c.SetRequest(c.Request().WithContext(newCtx))` 将更新后的 `Context` 写回 `echo.Context` + ,以便传递给后续的中间件和 `Controller`。 diff --git a/design/provide-logger-with-mothed/task-repository.md b/design/provide-logger-with-mothed/task-repository.md new file mode 100644 index 0000000..ab0fabf --- /dev/null +++ b/design/provide-logger-with-mothed/task-repository.md @@ -0,0 +1,396 @@ +- **`internal/infra/repository/unit_of_work.go` (`repository.UnitOfWork` - `gormUnitOfWork` 实现)** + - **接口改造 (`UnitOfWork`)**: + - [ ] 修改 `UnitOfWork` 接口中的 `ExecuteInTransaction` 方法签名,使其接收 `ctx context.Context` 作为第一个参数: + `ExecuteInTransaction(ctx context.Context, fn func(tx *gorm.DB) error) error`。 + - **结构体改造 (`gormUnitOfWork`)**: + - [ ] 移除 `gormUnitOfWork` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormUnitOfWork`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `gormUnitOfWork` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "GormUnitOfWork")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormUnitOfWork` 结构体的 `selfCtx` 成员。 + - **方法改造 (`ExecuteInTransaction`)**: + - [ ] 修改方法签名,使其接收 `ctx context.Context` 作为第一个参数: + `(u *gormUnitOfWork) ExecuteInTransaction(ctx context.Context, fn func(tx *gorm.DB) error) error`。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, u.selfCtx, "ExecuteInTransaction")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `u.logger.Errorf` 的调用替换为 `logger.Errorf`。 + - [ ] 在开启事务时,使用 `tx := u.db.WithContext(newCtx).Begin()`,确保事务 `tx` 携带了正确的上下文。 + +- **`internal/infra/repository/plan_repository.go` (`repository.PlanRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormPlanRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormPlanRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PlanRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormPlanRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/user_repository.go` (`repository.UserRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormUserRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormUserRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "UserRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormUserRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/device_repository.go` (`repository.DeviceRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormDeviceRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormDeviceRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "DeviceRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormDeviceRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/pig_pen_repository.go` (`repository.PigPenRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormPigPenRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormPigPenRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigPenRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormPigPenRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/pig_farm_repository.go` (`repository.PigFarmRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormPigFarmRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormPigFarmRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigFarmRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormPigFarmRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/pig_sick_repository.go` (`repository.PigSickLogRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormPigSickLogRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormPigSickLogRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigSickLogRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormPigSickLogRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/pig_batch_repository.go` (`repository.PigBatchRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormPigBatchRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormPigBatchRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigBatchRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormPigBatchRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/pig_trade_repository.go` (`repository.PigTradeRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormPigTradeRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormPigTradeRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigTradeRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormPigTradeRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/sensor_data_repository.go` (`repository.SensorDataRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormSensorDataRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormSensorDataRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "SensorDataRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormSensorDataRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/notification_repository.go` (`repository.NotificationRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormNotificationRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormNotificationRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "NotificationRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormNotificationRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/pending_task_repository.go` (`repository.PendingTaskRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormPendingTaskRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormPendingTaskRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PendingTaskRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormPendingTaskRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/raw_material_repository.go` (`repository.RawMaterialRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormRawMaterialRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormRawMaterialRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "RawMaterialRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormRawMaterialRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/execution_log_repository.go` (`repository.ExecutionLogRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormExecutionLogRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormExecutionLogRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "ExecutionLogRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormExecutionLogRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/pig_batch_log_repository.go` (`repository.PigBatchLogRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormPigBatchLogRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormPigBatchLogRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigBatchLogRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormPigBatchLogRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/medication_log_repository.go` (`repository.MedicationLogRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormMedicationLogRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormMedicationLogRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "MedicationLogRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormMedicationLogRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/area_controller_repository.go` (`repository.AreaControllerRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormAreaControllerRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormAreaControllerRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "AreaControllerRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormAreaControllerRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/device_template_repository.go` (`repository.DeviceTemplateRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormDeviceTemplateRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormDeviceTemplateRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "DeviceTemplateRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormDeviceTemplateRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/user_action_log_repository.go` (`repository.UserActionLogRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormUserActionLogRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormUserActionLogRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "UserActionLogRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormUserActionLogRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/pig_transfer_log_repository.go` (`repository.PigTransferLogRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormPigTransferLogRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormPigTransferLogRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigTransferLogRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormPigTransferLogRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/device_command_log_repository.go` (`repository.DeviceCommandLogRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormDeviceCommandLogRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormDeviceCommandLogRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "DeviceCommandLogRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormDeviceCommandLogRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/pending_collection_repository.go` (`repository.PendingCollectionRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormPendingCollectionRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormPendingCollectionRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PendingCollectionRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormPendingCollectionRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/raw_material_repository.go` (`repository.RawMaterialRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormRawMaterialRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormRawMaterialRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "RawMaterialRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormRawMaterialRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/execution_log_repository.go` (`repository.ExecutionLogRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormExecutionLogRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormExecutionLogRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "ExecutionLogRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormExecutionLogRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/pig_batch_log_repository.go` (`repository.PigBatchLogRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormPigBatchLogRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormPigBatchLogRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigBatchLogRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormPigBatchLogRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/medication_log_repository.go` (`repository.MedicationLogRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormMedicationLogRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormMedicationLogRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "MedicationLogRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormMedicationLogRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/area_controller_repository.go` (`repository.AreaControllerRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormAreaControllerRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormAreaControllerRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "AreaControllerRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormAreaControllerRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/device_template_repository.go` (`repository.DeviceTemplateRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormDeviceTemplateRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormDeviceTemplateRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "DeviceTemplateRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormDeviceTemplateRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/user_action_log_repository.go` (`repository.UserActionLogRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormUserActionLogRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormUserActionLogRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "UserActionLogRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormUserActionLogRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(newCtx)`。 + +- **`internal/infra/repository/pig_transfer_log_repository.go` (`repository.PigTransferLogRepository`)** + - **结构体改造**: + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewGormPigTransferLogRepository`)**: + - [ ] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在函数内部,为 `gormPigTransferLogRepository` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigTransferLogRepository")`。 + - [ ] 将这个 `selfCtx` 赋值给 `gormPigTransferLogRepository` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有公共方法)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx := logs.AddFuncName(ctx, r.selfCtx, "MethodName")`。 + - [ ] 确保所有对 `r.db` 和 `tx` 的调用都使用 `r.db.WithContext(newCtx)` 或 `tx.WithContext(new \ No newline at end of file diff --git a/design/provide-logger-with-mothed/task-service.md b/design/provide-logger-with-mothed/task-service.md new file mode 100644 index 0000000..7fdc0ca --- /dev/null +++ b/design/provide-logger-with-mothed/task-service.md @@ -0,0 +1,113 @@ +- **`internal/app/service/pig_farm_service.go` (`service.PigFarmService`)** + - **结构体改造**: + - [ ] 移除 `PigFarmService` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - [ ] 移除 `PigFarmService` 结构体中的 `repo repository.PigFarmRepository` 成员,改为 + `repo repository.PigFarmRepository`。 + - **构造函数改造 (`NewPigFarmService`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `PigFarmService` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigFarmService")`。 + - [ ] 将这个 `selfCtx` 赋值给 `PigFarmService` 结构体的 `selfCtx` 成员。 + - **公共方法改造 ( + 所有方法,例如 `CreatePigHouse`, `GetPigHouseByID`, `ListPigHouses`, `UpdatePigHouse`, `DeletePigHouse`, + `CreatePen`, `GetPenByID`, `ListPens`, `UpdatePen`, `DeletePen`, `UpdatePenStatus`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `s.logger.Errorf` 和 `s.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。 + - [ ] 确保所有对 `s.repo` 的调用都将 `newCtx` 作为第一个参数传递。 + +- **`internal/app/service/pig_batch_service.go` (`service.PigBatchService`)** + - **结构体改造**: + - [ ] 移除 `PigBatchService` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewPigBatchService`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `PigBatchService` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PigBatchService")`。 + - [ ] 将这个 `selfCtx` 赋值给 `PigBatchService` 结构体的 `selfCtx` 成员。 + - **公共方法改造 ( + 所有方法,例如 `CreatePigBatch`, `GetPigBatch`, `UpdatePigBatch`, `DeletePigBatch`, `ListPigBatches`, + `AssignEmptyPensToBatch`, `ReclassifyPenToNewBatch`, `RemoveEmptyPenFromBatch`, `MovePigsIntoPen`, `SellPigs`, + `BuyPigs`, `RecordSickPigs`, `RecordSickPigRecovery`, `RecordSickPigDeath`, `RecordSickPigCull`, `RecordDeath`, + `RecordCull`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `s.logger.Errorf` 和 `s.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。 + - [ ] 确保所有对 `s.pigBatchRepo`, `s.pigBatchLogRepo`, `s.uow`, `s.transferSvc`, `s.tradeSvc`, `s.sickSvc` + 等依赖的调用都将 `newCtx` 作为第一个参数传递。 + +- **`internal/app/service/monitor_service.go` (`service.MonitorService`)** + - **结构体改造**: + - [ ] 移除 `MonitorService` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewMonitorService`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `MonitorService` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "MonitorService")`。 + - [ ] 将这个 `selfCtx` 赋值给 `MonitorService` 结构体的 `selfCtx` 成员。 + - **公共方法改造 ( + 所有方法,例如 `ListSensorData`, `ListDeviceCommandLogs`, `ListPlanExecutionLogs`, `ListTaskExecutionLogs`, + `ListPendingCollections`, `ListUserActionLogs`, `ListRawMaterialPurchases`, `ListRawMaterialStockLogs`, + `ListFeedUsageRecords`, `ListMedicationLogs`, `ListPigBatchLogs`, `ListWeighingBatches`, `ListWeighingRecords`, + `ListPigTransferLogs`, `ListPigSickLogs`, `ListPigPurchases`, `ListPigSales`, `ListNotifications`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `s.logger.Errorf` 和 `s.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。 + - [ ] 确保所有对 `s.repo` 的调用都将 `newCtx` 作为第一个参数传递。 + +- **`internal/app/service/device_service.go` (`service.DeviceService`)** + - **结构体改造**: + - [ ] 移除 `DeviceService` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewDeviceService`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `DeviceService` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "DeviceService")`。 + - [ ] 将这个 `selfCtx` 赋值给 `DeviceService` 结构体的 `selfCtx` 成员。 + - **公共方法改造 ( + 所有方法,例如 `CreateDevice`, `GetDevice`, `ListDevices`, `UpdateDevice`, `DeleteDevice`, `ManualControl`, + `CreateAreaController`, `GetAreaController`, `ListAreaControllers`, `UpdateAreaController`, + `DeleteAreaController`, `CreateDeviceTemplate`, `GetDeviceTemplate`, `ListDeviceTemplates`, + `UpdateDeviceTemplate`, `DeleteDeviceTemplate`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `s.logger.Errorf`, `s.logger.Warnf`, `s.logger.Infof` 的调用替换为 `logger.Errorf`, + `logger.Warnf`, `logger.Infof`。 + - [ ] 确保所有对 `s.repo`, `s.generalDeviceService` 等依赖的调用都将 `newCtx` 作为第一个参数传递。 + +- **`internal/app/service/plan_service.go` (`service.PlanService`)** + - **结构体改造**: + - [ ] 移除 `PlanService` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewPlanService`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `PlanService` 创建其专属的 `selfCtx`:`selfCtx := logs.AddCompName(ctx, "PlanService")`。 + - [ ] 将这个 `selfCtx` 赋值给 `PlanService` 结构体的 `selfCtx` 成员。 + - **公共方法改造 ( + 所有方法,例如 `CreatePlan`, `GetPlanByID`, `ListPlans`, `UpdatePlan`, `DeletePlan`, `StartPlan`, `StopPlan`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `s.logger.Errorf` 和 `s.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。 + - [ ] 确保所有对 `s.repo`, `s.planExecutionManager`, `s.analysisPlanTaskManager` 等依赖的调用都将 `newCtx` + 作为第一个参数传递。 + +- **`internal/app/service/user_service.go` (`service.UserService`)** + - **结构体改造**: + - [ ] 移除 `UserService` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewUserService`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `UserService` 创建其专属的 `selfCtx`:`selfCtx := logs.AddCompName(ctx, "UserService")`。 + - [ ] 将这个 `selfCtx` 赋值给 `UserService` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (所有方法,例如 `CreateUser`, `Login`, `SendTestNotification`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `s.logger.Errorf` 和 `s.logger.Infof` 的调用替换为 `logger.Errorf` 和 `logger.Infof`。 + - [ ] 确保所有对 `s.repo`, `s.tokenService`, `s.notifier` 等依赖的调用都将 `newCtx` 作为第一个参数传递。 \ No newline at end of file diff --git a/design/provide-logger-with-mothed/task-webhook.md b/design/provide-logger-with-mothed/task-webhook.md new file mode 100644 index 0000000..d6e42d8 --- /dev/null +++ b/design/provide-logger-with-mothed/task-webhook.md @@ -0,0 +1,39 @@ +- **`internal/app/webhook/chirp_stack.go` (`webhook.ChirpStackListener`)** + - **结构体改造**: + - [ ] 移除 `ChirpStackListener` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewChirpStackListener`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `ChirpStackListener` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "ChirpStackListener")`。 + - [ ] 将这个 `selfCtx` 赋值给 `ChirpStackListener` 结构体的 `selfCtx` 成员。 + - **公共方法改造 (`Handler`)**: + - [ ] `Handler` 方法返回 `http.HandlerFunc`。在返回的 `http.HandlerFunc` 内部,获取 `r.Context()` 作为 + `upstreamCtx`。 + - [ ] 在 `go c.handler(b, event)` 调用之前,将 `upstreamCtx` 传递给 `c.handler` 方法,即 + `go c.handler(upstreamCtx, b, event)`。 + - [ ] 将所有对 `c.logger.Errorf` 的调用替换为 `logger.Errorf` (在 `handler` 方法中处理)。 + - **内部辅助方法改造 (`handler`, `handleUpEvent`, `handleStatusEvent`, `handleAckEvent`, `handleLogEvent`, + `handleJoinEvent`, `handleTxAckEvent`, `handleLocationEvent`, `handleIntegrationEvent`, `recordSensorData`)**: + - [ ] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。 + - [ ] 在每个方法入口处,使用 `newCtx, logger := logs.Trace(ctx, c.selfCtx, "MethodName")` 获取新的 + `context.Context` 和 `logger` 实例。 + - [ ] 将所有对 `c.logger.Errorf`, `c.logger.Infof`, `c.logger.Warnf` 的调用替换为 `logger.Errorf`, + `logger.Infof`, `logger.Warnf`。 + - [ ] 确保所有对 `c.sensorDataRepo`, `c.deviceRepo`, `c.areaControllerRepo`, `c.deviceCommandLogRepo`, + `c.pendingCollectionRepo` 等依赖的调用都将 `newCtx` 作为第一个参数传递。 +- **`internal/app/webhook/placeholder_listener.go` (`webhook.PlaceholderListener`)** + - **结构体改造**: + - [ ] 移除 `PlaceholderListener` 结构体中的 `logger *logs.Logger` 成员。 + - [ ] 新增 `selfCtx context.Context` 成员。 + - **构造函数改造 (`NewPlaceholderListener`)**: + - [ ] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`。 + - [ ] 在函数内部,为 `PlaceholderListener` 创建其专属的 `selfCtx`: + `selfCtx := logs.AddCompName(ctx, "PlaceholderListener")`。 + - [ ] 将这个 `selfCtx` 赋值给 `PlaceholderListener` 结构体的 `selfCtx` 成员。 + - [ ] 使用 `newCtx, logger := logs.Trace(ctx, selfCtx, "NewPlaceholderListener")` 获取 `logger` 实例,并替换 + `logger.Info` 调用。 + - **公共方法改造 (`Handler`)**: + - [ ] 在 `Handler` 方法返回的 `http.HandlerFunc` 内部,获取 `r.Context()` 作为 `upstreamCtx`。 + - [ ] 使用 `newCtx, logger := logs.Trace(upstreamCtx, p.selfCtx, "Handler")` 获取 `logger` 实例。 + - [ ] 将所有对 `p.logger.Warn` 的调用替换为 `logger.Warn`。 \ No newline at end of file