Skip to content

Commit

Permalink
feat:add log&error msg
Browse files Browse the repository at this point in the history
  • Loading branch information
nick887 committed Oct 5, 2021
1 parent c234345 commit 111abc2
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 17 deletions.
38 changes: 23 additions & 15 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,55 @@ package adapter
import (
"github.com/Chronostasys/centralog/centralog"
"github.com/gin-gonic/gin"
"github.com/nick887/customize_router/http_status"
"net/http"
"time"
)

// @author Ritchie
// @version 2.0, 2021/10/4
// @since 2.0.0
// @version 1.0, 2021/10/4
// @since 1.0.0

// RequestHandler is a type which give the router a restriction
type RequestHandler func(ctx *gin.Context) (status int, json interface{}, err error)
type RequestHandler func(ctx *gin.Context) (httpStatus *http_status.HttpStatus, json interface{})

// ResponseError is a unified response of error
type ResponseError struct {
Time time.Time `json:"time"`
Reason string `json:"reason"`
RequestId string `json:"request_id"`
Time time.Time `json:"time"`
Reason interface{} `json:"reason"`
RequestId string `json:"request_id"`
Code int `json:"code"`
}

// ErrorAdapter is wrapper of handler
func ErrorAdapter(handler RequestHandler) gin.HandlerFunc {
return func(ctx *gin.Context) {
status, json, err := handler(ctx)
processResult(ctx, status, json, err)
httpStatus, json := handler(ctx)
processResult(ctx, httpStatus, json)
}
}

// processResult process the error msg
func processResult(ctx *gin.Context, status int, json interface{}, err error) {
if err != nil {
centralog.Error("[request error]").Any("error", err).CtxID(ctx).Log()
ctx.AbortWithStatusJSON(status, generateErrorMsg(ctx, err))
func processResult(ctx *gin.Context, httpStatus *http_status.HttpStatus, json interface{}) {
if httpStatus.Code != 200 {
centralog.Error("[request error]").Any("error", httpStatus.Msg).CtxID(ctx).Log()
if httpStatus.Code >= 1000 {
ctx.AbortWithStatusJSON(http.StatusInternalServerError, generateErrorMsg(ctx, httpStatus))
return
}
ctx.AbortWithStatusJSON(httpStatus.Code, generateErrorMsg(ctx, httpStatus))
return
}
ctx.JSON(status, json)
ctx.JSON(httpStatus.Code, json)
}

// generateErrorMsg generate the error msg
func generateErrorMsg(ctx *gin.Context, err error) ResponseError {
func generateErrorMsg(ctx *gin.Context, httpStatus *http_status.HttpStatus) ResponseError {
requestId, _ := ctx.Get(centralog.IDKey)
return ResponseError{
Time: time.Now(),
Reason: err.Error(),
Reason: httpStatus.Msg,
RequestId: requestId.(string),
Code: httpStatus.Code,
}
}
9 changes: 9 additions & 0 deletions common/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package common

// @author Ritchie
// @version 1.0, 2021/10/5
// @since 1.0.0

var (
REQUEST_ID = "request_id"
)
4 changes: 2 additions & 2 deletions custom/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

// @author Ritchie
// @version 2.0, 2021/10/4
// @since 2.0.0
// @version 1.0, 2021/10/4
// @since 1.0.0

// CustmoRouter is the router we define
type CustmoRouter struct {
Expand Down
10 changes: 10 additions & 0 deletions http_status/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package http_status

// @author Ritchie
// @version 2.0, 2021/10/5
// @since 2.0.0

type HttpStatus struct {
Msg interface{}
Code int
}
21 changes: 21 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package customize_router

import (
"github.com/Chronostasys/centralog/centralog"
"github.com/nick887/customize_router/custom"
"github.com/nick887/customize_router/log"
"go.uber.org/zap"
)

// @author Ritchie
// @version 1.0, 2021/10/5
// @since 1.0.0


func InitCustomRouter(logOpt *centralog.LogOptions,cr *custom.CustmoRouter) {
err := centralog.InitLoggerWithOpt(zap.NewProductionConfig(), logOpt)
if err != nil {
panic(err)
}
cr.Use(log.Log())
}
42 changes: 42 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package log

import (
"fmt"
"github.com/Chronostasys/centralog/centralog"
"github.com/gin-gonic/gin"
"github.com/nick887/customize_router/common"
"time"
"github.com/google/uuid"
)

// @author Ritchie
// @version 1.0, 2021/10/5
// @since 1.0.0


func Log() gin.HandlerFunc {
return func(c *gin.Context) {
// request id
requestId := c.GetHeader(common.REQUEST_ID)
var id string
if requestId == "" {
id = uuid.New().String()
c.Header(common.REQUEST_ID, id)
} else {
id = c.GetHeader(common.REQUEST_ID)
}
method := c.Request.Method
url := c.Request.URL.RequestURI()
c.Set(centralog.IDKey, id)
defer centralog.Sync()
start := time.Now()
c.Next()
centralog.Info(method).
Any("elapsed",
fmt.Sprintf("%f", float64(time.Since(start).Microseconds())/1000)+"ms").
Any("url", url).
Any("ip", c.ClientIP()).
CtxID(c).
Log()
}
}

0 comments on commit 111abc2

Please sign in to comment.