Skip to content

Commit

Permalink
fix log (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
huweihuang authored Nov 10, 2023
1 parent 29f82d7 commit 5ba4e80
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
11 changes: 5 additions & 6 deletions gin/middlewares/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func ErrorWrapper(c *gin.Context, msg string, err error) {
Data: map[string]interface{}{"error": err.Error()},
}
log.Log().WithField("resp", resp).Error(msg)
c.JSON(http.StatusInternalServerError, resp)
c.AbortWithStatusJSON(http.StatusInternalServerError, resp)
}

// NotFoundWrapper 封装NotFound的处理逻辑,状态码 404
Expand All @@ -41,7 +41,7 @@ func NotFoundWrapper(c *gin.Context, msg string, data interface{}) {
Data: data,
}
log.Log().WithField("resp", resp).Error(msg)
c.JSON(http.StatusNotFound, resp)
c.AbortWithStatusJSON(http.StatusNotFound, resp)
}

// BadRequestWrapper 封装非法请求的处理逻辑,状态码 400
Expand All @@ -52,7 +52,7 @@ func BadRequestWrapper(c *gin.Context, err error) {
Data: map[string]interface{}{"error": err.Error()},
}
log.Log().WithField("resp", resp).Error("invalid request")
c.JSON(http.StatusBadRequest, resp)
c.AbortWithStatusJSON(http.StatusBadRequest, resp)
}

// ValidateBadRequestWrapper 封装多项校验非法请求的处理逻辑,状态码 400
Expand All @@ -63,7 +63,7 @@ func ValidateBadRequestWrapper(c *gin.Context, errs field.ErrorList) {
Data: map[string]interface{}{"error": errs},
}
log.Log().WithField("resp", resp).Error("invalid request")
c.JSON(http.StatusBadRequest, resp)
c.AbortWithStatusJSON(http.StatusBadRequest, resp)
}

func ParseRequest(c *gin.Context, request interface{}) {
Expand All @@ -74,7 +74,6 @@ func ParseRequest(c *gin.Context, request interface{}) {
Data: map[string]interface{}{"error": err},
}
log.Log().WithField("err", err).Warn("invalid request body")
c.JSON(http.StatusBadRequest, resp)
return
c.AbortWithStatusJSON(http.StatusBadRequest, resp)
}
}
2 changes: 1 addition & 1 deletion httplib/httplib.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func CallURL(method, url, path string, header map[string]string, request interfa
"path": path,
"header": header,
"request": request,
}).Info("request url info")
}).Debug("request url info")

params, err := encodeData(request)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions kube/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kube

import (
"fmt"
"sort"
"strings"
)

func ConvertMapToStr(toConvertMap map[string]string) string {
str := make([]string, len(toConvertMap))
i := 0
for name, value := range toConvertMap {
str[i] = fmt.Sprintf("%s=%s", name, value)
i++
}
sort.Strings(str)
return strings.Join(str, ",")
}
2 changes: 1 addition & 1 deletion logger/logrus/logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
const (
defaultLevel = "info"
defaultLogFile = "log/info.log"
defaultFormat = "text"
defaultFormat = "json"
defaultEnableForceColors = true
)

Expand Down

0 comments on commit 5ba4e80

Please sign in to comment.