调整日志等级
This commit is contained in:
		| @@ -12,6 +12,7 @@ import ( | ||||
| // setupRoutes 设置所有 API 路由 | ||||
| // 在此方法中,使用已初始化的控制器实例将其路由注册到 Gin 引擎中。 | ||||
| func (a *API) setupRoutes() { | ||||
| 	a.logger.Info("开始初始化所有 API 路由") | ||||
|  | ||||
| 	// --- Public Routes --- | ||||
| 	// 这些路由不需要身份验证 | ||||
| @@ -19,7 +20,7 @@ func (a *API) setupRoutes() { | ||||
| 	// 用户注册和登录 | ||||
| 	a.engine.POST("/api/v1/users", a.userController.CreateUser)  // 注册新用户 | ||||
| 	a.engine.POST("/api/v1/users/login", a.userController.Login) // 用户登录 | ||||
| 	a.logger.Info("公开接口注册成功:用户注册、登录") | ||||
| 	a.logger.Debug("公开接口注册成功:用户注册、登录") | ||||
|  | ||||
| 	// 注册 pprof 路由 | ||||
| 	pprofGroup := a.engine.Group("/debug/pprof") | ||||
| @@ -37,15 +38,15 @@ func (a *API) setupRoutes() { | ||||
| 		pprofGroup.GET("/mutex", gin.WrapH(pprof.Handler("mutex"))) // pprof 互斥锁 | ||||
| 		pprofGroup.GET("/threadcreate", gin.WrapH(pprof.Handler("threadcreate"))) | ||||
| 	} | ||||
| 	a.logger.Info("pprof 接口注册成功") | ||||
| 	a.logger.Debug("pprof 接口注册成功") | ||||
|  | ||||
| 	// 上行事件监听路由 | ||||
| 	a.engine.POST("/upstream", gin.WrapH(a.listenHandler.Handler())) // 处理设备上行事件 | ||||
| 	a.logger.Info("上行事件监听接口注册成功") | ||||
| 	a.logger.Debug("上行事件监听接口注册成功") | ||||
|  | ||||
| 	// 添加 Swagger UI 路由, Swagger UI可在 /swagger/index.html 上找到 | ||||
| 	a.engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) // Swagger UI 接口 | ||||
| 	a.logger.Info("Swagger UI 接口注册成功") | ||||
| 	a.logger.Debug("Swagger UI 接口注册成功") | ||||
|  | ||||
| 	// --- Authenticated Routes --- | ||||
| 	// 所有在此注册的路由都需要通过 JWT 身份验证 | ||||
| @@ -59,7 +60,7 @@ func (a *API) setupRoutes() { | ||||
| 			userGroup.GET("/:id/history", a.userController.ListUserHistory) // 获取用户操作历史 | ||||
| 			userGroup.POST("/:id/notifications/test", a.userController.SendTestNotification) | ||||
| 		} | ||||
| 		a.logger.Info("用户相关接口注册成功 (需要认证和审计)") | ||||
| 		a.logger.Debug("用户相关接口注册成功 (需要认证和审计)") | ||||
|  | ||||
| 		// 设备相关路由组 | ||||
| 		deviceGroup := authGroup.Group("/devices") | ||||
| @@ -71,7 +72,7 @@ func (a *API) setupRoutes() { | ||||
| 			deviceGroup.DELETE("/:id", a.deviceController.DeleteDevice)               // 删除设备 | ||||
| 			deviceGroup.POST("/manual-control/:id", a.deviceController.ManualControl) // 手动控制设备 | ||||
| 		} | ||||
| 		a.logger.Info("设备相关接口注册成功 (需要认证和审计)") | ||||
| 		a.logger.Debug("设备相关接口注册成功 (需要认证和审计)") | ||||
|  | ||||
| 		// 区域主控相关路由组 | ||||
| 		areaControllerGroup := authGroup.Group("/area-controllers") | ||||
| @@ -82,7 +83,7 @@ func (a *API) setupRoutes() { | ||||
| 			areaControllerGroup.PUT("/:id", a.deviceController.UpdateAreaController)    // 更新区域主控 | ||||
| 			areaControllerGroup.DELETE("/:id", a.deviceController.DeleteAreaController) // 删除区域主控 | ||||
| 		} | ||||
| 		a.logger.Info("区域主控相关接口注册成功 (需要认证和审计)") | ||||
| 		a.logger.Debug("区域主控相关接口注册成功 (需要认证和审计)") | ||||
|  | ||||
| 		// 设备模板相关路由组 | ||||
| 		deviceTemplateGroup := authGroup.Group("/device-templates") | ||||
| @@ -93,7 +94,7 @@ func (a *API) setupRoutes() { | ||||
| 			deviceTemplateGroup.PUT("/:id", a.deviceController.UpdateDeviceTemplate)    // 更新设备模板 | ||||
| 			deviceTemplateGroup.DELETE("/:id", a.deviceController.DeleteDeviceTemplate) // 删除设备模板 | ||||
| 		} | ||||
| 		a.logger.Info("设备模板相关接口注册成功 (需要认证和审计)") | ||||
| 		a.logger.Debug("设备模板相关接口注册成功 (需要认证和审计)") | ||||
|  | ||||
| 		// 计划相关路由组 | ||||
| 		planGroup := authGroup.Group("/plans") | ||||
| @@ -106,7 +107,7 @@ func (a *API) setupRoutes() { | ||||
| 			planGroup.POST("/:id/start", a.planController.StartPlan) // 启动计划 | ||||
| 			planGroup.POST("/:id/stop", a.planController.StopPlan)   // 停止计划 | ||||
| 		} | ||||
| 		a.logger.Info("计划相关接口注册成功 (需要认证和审计)") | ||||
| 		a.logger.Debug("计划相关接口注册成功 (需要认证和审计)") | ||||
|  | ||||
| 		// 猪舍相关路由组 | ||||
| 		pigHouseGroup := authGroup.Group("/pig-houses") | ||||
| @@ -117,7 +118,7 @@ func (a *API) setupRoutes() { | ||||
| 			pigHouseGroup.PUT("/:id", a.pigFarmController.UpdatePigHouse)    // 更新猪舍 | ||||
| 			pigHouseGroup.DELETE("/:id", a.pigFarmController.DeletePigHouse) // 删除猪舍 | ||||
| 		} | ||||
| 		a.logger.Info("猪舍相关接口注册成功 (需要认证和审计)") | ||||
| 		a.logger.Debug("猪舍相关接口注册成功 (需要认证和审计)") | ||||
|  | ||||
| 		// 猪圈相关路由组 | ||||
| 		penGroup := authGroup.Group("/pens") | ||||
| @@ -129,7 +130,7 @@ func (a *API) setupRoutes() { | ||||
| 			penGroup.DELETE("/:id", a.pigFarmController.DeletePen)           // 删除猪圈 | ||||
| 			penGroup.PUT("/:id/status", a.pigFarmController.UpdatePenStatus) // 更新猪圈状态 | ||||
| 		} | ||||
| 		a.logger.Info("猪圈相关接口注册成功 (需要认证和审计)") | ||||
| 		a.logger.Debug("猪圈相关接口注册成功 (需要认证和审计)") | ||||
|  | ||||
| 		// 猪群相关路由组 | ||||
| 		pigBatchGroup := authGroup.Group("/pig-batches") | ||||
| @@ -154,7 +155,7 @@ func (a *API) setupRoutes() { | ||||
| 			pigBatchGroup.POST("/record-death/:id", a.pigBatchController.RecordDeath)                                     // 记录正常猪只死亡事件 | ||||
| 			pigBatchGroup.POST("/record-cull/:id", a.pigBatchController.RecordCull)                                       // 记录正常猪只淘汰事件 | ||||
| 		} | ||||
| 		a.logger.Info("猪群相关接口注册成功 (需要认证和审计)") | ||||
| 		a.logger.Debug("猪群相关接口注册成功 (需要认证和审计)") | ||||
|  | ||||
| 		// 数据监控相关路由组 | ||||
| 		monitorGroup := authGroup.Group("/monitor") | ||||
| @@ -178,6 +179,8 @@ func (a *API) setupRoutes() { | ||||
| 			monitorGroup.GET("/pig-sales", a.monitorController.ListPigSales) | ||||
| 			monitorGroup.GET("/notifications", a.monitorController.ListNotifications) | ||||
| 		} | ||||
| 		a.logger.Info("数据监控相关接口注册成功 (需要认证和审计)") | ||||
| 		a.logger.Debug("数据监控相关接口注册成功 (需要认证和审计)") | ||||
| 	} | ||||
|  | ||||
| 	a.logger.Debug("所有接口注册成功") | ||||
| } | ||||
|   | ||||
| @@ -177,13 +177,13 @@ func (ps *PostgresStorage) creatingHyperTable() error { | ||||
| 	for _, table := range tablesToConvert { | ||||
| 		tableName := table.model.TableName() | ||||
| 		chunkInterval := "1 days" // 统一设置为1天 | ||||
| 		ps.logger.Infow("准备将表转换为超表", "table", tableName, "chunk_interval", chunkInterval) | ||||
| 		ps.logger.Debugw("准备将表转换为超表", "table", tableName, "chunk_interval", chunkInterval) | ||||
| 		sql := fmt.Sprintf("SELECT create_hypertable('%s', '%s', chunk_time_interval => INTERVAL '%s', if_not_exists => TRUE);", tableName, table.timeColumn, chunkInterval) | ||||
| 		if err := ps.db.Exec(sql).Error; err != nil { | ||||
| 			ps.logger.Errorw("转换为超表失败", "table", tableName, "error", err) | ||||
| 			return fmt.Errorf("将 %s 转换为超表失败: %w", tableName, err) | ||||
| 		} | ||||
| 		ps.logger.Infow("成功将表转换为超表 (或已转换)", "table", tableName) | ||||
| 		ps.logger.Debugw("成功将表转换为超表 (或已转换)", "table", tableName) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| @@ -220,22 +220,23 @@ func (ps *PostgresStorage) applyCompressionPolicies() error { | ||||
| 		compressAfter := "3 days" // 统一设置为2天后(即进入第3天)开始压缩 | ||||
|  | ||||
| 		// 1. 开启表的压缩设置,并指定分段列 | ||||
| 		ps.logger.Infow("为表启用压缩设置", "table", tableName, "segment_by", policy.segmentColumn) | ||||
| 		ps.logger.Debugw("为表启用压缩设置", "table", tableName, "segment_by", policy.segmentColumn) | ||||
| 		// 使用 + 而非Sprintf以规避goland静态检查报错 | ||||
| 		alterSQL := "ALTER TABLE" + " " + tableName + " SET (timescaledb.compress, timescaledb.compress_segmentby = '" + policy.segmentColumn + "');" | ||||
| 		if err := ps.db.Exec(alterSQL).Error; err != nil { | ||||
| 			// 忽略错误,因为这个设置可能是不可变的,重复执行会报错 | ||||
| 			ps.logger.Warnw("启用压缩设置时遇到问题 (可能已设置,可忽略)", "table", tableName, "error", err) | ||||
| 		} | ||||
| 		ps.logger.Debugw("成功为表启用压缩设置 (或已启用)", "table", tableName) | ||||
|  | ||||
| 		// 2. 添加压缩策略 | ||||
| 		ps.logger.Infow("为表添加压缩策略", "table", tableName, "compress_after", compressAfter) | ||||
| 		ps.logger.Debugw("为表添加压缩策略", "table", tableName, "compress_after", compressAfter) | ||||
| 		policySQL := fmt.Sprintf("SELECT add_compression_policy('%s', INTERVAL '%s', if_not_exists => TRUE);", tableName, compressAfter) | ||||
| 		if err := ps.db.Exec(policySQL).Error; err != nil { | ||||
| 			ps.logger.Errorw("添加压缩策略失败", "table", tableName, "error", err) | ||||
| 			return fmt.Errorf("为 %s 添加压缩策略失败: %w", tableName, err) | ||||
| 		} | ||||
| 		ps.logger.Infow("成功为表添加压缩策略 (或已存在)", "table", tableName) | ||||
| 		ps.logger.Debugw("成功为表添加压缩策略 (或已存在)", "table", tableName) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| @@ -247,22 +248,22 @@ func (ps *PostgresStorage) creatingIndex() error { | ||||
| 	// 如果索引已存在,此命令不会报错 | ||||
|  | ||||
| 	// 为 sensor_data 表的 data 字段创建 GIN 索引 | ||||
| 	ps.logger.Info("正在为 sensor_data 表的 data 字段创建 GIN 索引") | ||||
| 	ps.logger.Debug("正在为 sensor_data 表的 data 字段创建 GIN 索引") | ||||
| 	ginSensorDataIndexSQL := "CREATE INDEX IF NOT EXISTS idx_sensor_data_data_gin ON sensor_data USING GIN (data);" | ||||
| 	if err := ps.db.Exec(ginSensorDataIndexSQL).Error; err != nil { | ||||
| 		ps.logger.Errorw("为 sensor_data 的 data 字段创建 GIN 索引失败", "error", err) | ||||
| 		return fmt.Errorf("为 sensor_data 的 data 字段创建 GIN 索引失败: %w", err) | ||||
| 	} | ||||
| 	ps.logger.Info("成功为 sensor_data 的 data 字段创建 GIN 索引 (或已存在)") | ||||
| 	ps.logger.Debug("成功为 sensor_data 的 data 字段创建 GIN 索引 (或已存在)") | ||||
|  | ||||
| 	// 为 tasks.parameters 创建 GIN 索引 | ||||
| 	ps.logger.Info("正在为 tasks 表的 parameters 字段创建 GIN 索引") | ||||
| 	ps.logger.Debug("正在为 tasks 表的 parameters 字段创建 GIN 索引") | ||||
| 	taskGinIndexSQL := "CREATE INDEX IF NOT EXISTS idx_tasks_parameters_gin ON tasks USING GIN (parameters);" | ||||
| 	if err := ps.db.Exec(taskGinIndexSQL).Error; err != nil { | ||||
| 		ps.logger.Errorw("为 tasks 的 parameters 字段创建 GIN 索引失败", "error", err) | ||||
| 		return fmt.Errorf("为 tasks 的 parameters 字段创建 GIN 索引失败: %w", err) | ||||
| 	} | ||||
| 	ps.logger.Info("成功为 tasks 的 parameters 字段创建 GIN 索引 (或已存在)") | ||||
| 	ps.logger.Debug("成功为 tasks 的 parameters 字段创建 GIN 索引 (或已存在)") | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user