add
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
||||
"net/http"
|
||||
"country/db"
|
||||
"time"
|
||||
"github.com/google/uuid" // 补充uuid导入(原代码使用但未显示导入)
|
||||
"github.com/google/uuid"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@@ -33,11 +33,11 @@ func CreateHandler(c *gin.Context) {
|
||||
reqID := c.Request.Header.Get("X-RegisterRequest-ID")
|
||||
if reqID == "" {
|
||||
reqID = uuid.New().String()
|
||||
zap.L().Debug("生成新的请求ID", zap.String("req_id", reqID))
|
||||
zap.L().Debug("✨ 生成新的请求ID", zap.String("req_id", reqID))
|
||||
}
|
||||
|
||||
// 标准化日志字段,增加请求路径和方法
|
||||
zap.L().Info("收到国家创建请求",
|
||||
zap.L().Info("📥 收到国家创建请求",
|
||||
zap.String("req_id", reqID),
|
||||
zap.String("path", c.Request.URL.Path),
|
||||
zap.String("method", c.Request.Method),
|
||||
@@ -46,7 +46,7 @@ func CreateHandler(c *gin.Context) {
|
||||
var req CreateRequest
|
||||
// 绑定并验证请求参数
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
zap.L().Warn("请求参数验证失败",
|
||||
zap.L().Warn("⚠️ 请求参数验证失败",
|
||||
zap.String("req_id", reqID),
|
||||
zap.Error(err),
|
||||
zap.Any("request_body", c.Request.Body),
|
||||
@@ -59,7 +59,7 @@ func CreateHandler(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 记录有效请求参数(敏感信息需脱敏,此处假设name和code非敏感)
|
||||
zap.L().Debug("请求参数验证通过",
|
||||
zap.L().Debug("✅ 请求参数验证通过",
|
||||
zap.String("req_id", reqID),
|
||||
zap.String("name", req.Name),
|
||||
zap.String("code", req.Code),
|
||||
@@ -68,7 +68,7 @@ func CreateHandler(c *gin.Context) {
|
||||
// 开启事务(将事务提前,确保country表插入也在事务中)
|
||||
tx, err := db.DB.Begin()
|
||||
if err != nil {
|
||||
zap.L().Error("事务开启失败",
|
||||
zap.L().Error("❌ 事务开启失败",
|
||||
zap.String("req_id", reqID),
|
||||
zap.Error(err),
|
||||
)
|
||||
@@ -82,12 +82,12 @@ func CreateHandler(c *gin.Context) {
|
||||
// 发生panic时回滚事务
|
||||
if r := recover(); r != nil {
|
||||
if err := tx.Rollback(); err != nil {
|
||||
zap.L().Error("panic后事务回滚失败",
|
||||
zap.L().Error("💥 panic后事务回滚失败",
|
||||
zap.String("req_id", reqID),
|
||||
zap.Error(err),
|
||||
)
|
||||
}
|
||||
zap.L().Error("事务处理发生panic",
|
||||
zap.L().Error("💥 事务处理发生panic",
|
||||
zap.String("req_id", reqID),
|
||||
zap.Any("recover", r),
|
||||
)
|
||||
@@ -103,7 +103,7 @@ func CreateHandler(c *gin.Context) {
|
||||
err = tx.QueryRow("INSERT INTO \"country\" DEFAULT VALUES RETURNING id").Scan(&countryID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
zap.L().Error("country表插入失败",
|
||||
zap.L().Error("❌ country表插入失败",
|
||||
zap.String("req_id", reqID),
|
||||
zap.Error(err),
|
||||
)
|
||||
@@ -114,7 +114,7 @@ func CreateHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
zap.L().Debug("country表插入成功",
|
||||
zap.L().Debug("📝 country表插入成功",
|
||||
zap.String("req_id", reqID),
|
||||
zap.String("country_id", countryID),
|
||||
)
|
||||
@@ -123,7 +123,7 @@ func CreateHandler(c *gin.Context) {
|
||||
_, err = tx.Exec("INSERT INTO name (country_id, name) VALUES ($1, $2)", countryID, req.Name)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
zap.L().Error("name表插入失败",
|
||||
zap.L().Error("❌ name表插入失败",
|
||||
zap.String("req_id", reqID),
|
||||
zap.String("country_id", countryID),
|
||||
zap.Error(err),
|
||||
@@ -139,7 +139,7 @@ func CreateHandler(c *gin.Context) {
|
||||
_, err = tx.Exec("INSERT INTO code (country_id, code) VALUES ($1, $2)", countryID, req.Code)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
zap.L().Error("code表插入失败",
|
||||
zap.L().Error("❌ code表插入失败",
|
||||
zap.String("req_id", reqID),
|
||||
zap.String("country_id", countryID),
|
||||
zap.Error(err),
|
||||
@@ -154,7 +154,7 @@ func CreateHandler(c *gin.Context) {
|
||||
// 提交事务
|
||||
if err := tx.Commit(); err != nil {
|
||||
tx.Rollback() // 尝试回滚提交失败的事务
|
||||
zap.L().Error("事务提交失败",
|
||||
zap.L().Error("❌ 事务提交失败",
|
||||
zap.String("req_id", reqID),
|
||||
zap.String("country_id", countryID),
|
||||
zap.Error(err),
|
||||
@@ -168,7 +168,7 @@ func CreateHandler(c *gin.Context) {
|
||||
|
||||
// 记录请求处理耗时
|
||||
duration := time.Since(startTime)
|
||||
zap.L().Info("国家创建请求处理完成",
|
||||
zap.L().Info("✅ 国家创建请求处理完成",
|
||||
zap.String("req_id", reqID),
|
||||
zap.String("country_id", countryID),
|
||||
zap.Duration("duration", duration),
|
||||
|
||||
Reference in New Issue
Block a user