Skip to content

Commit

Permalink
server http 增加访问日志
Browse files Browse the repository at this point in the history
  • Loading branch information
yangguang8131 committed Jun 1, 2021
1 parent 557c309 commit 6a35f93
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
3 changes: 3 additions & 0 deletions util/service/server_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ func shouldLogRequest(fullMethod string) bool {
return true
}
center := GetConfigCenter()
if center == nil {
return true
}
printBodyMethod := printBodyMethod{}

// 方法配置
Expand Down
64 changes: 62 additions & 2 deletions util/service/server_http.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package rocserv

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"regexp"
Expand All @@ -12,6 +14,10 @@ import (
"strings"
"time"

"gitlab.pri.ibanyu.com/middleware/seaweed/xtime"

"gitlab.pri.ibanyu.com/middleware/seaweed/xlog"

"gitlab.pri.ibanyu.com/middleware/seaweed/xcontext"
xprom "gitlab.pri.ibanyu.com/middleware/seaweed/xstat/xmetric/xprometheus"
"gitlab.pri.ibanyu.com/middleware/seaweed/xtrace"
Expand Down Expand Up @@ -53,7 +59,7 @@ type HandlerFunc func(*Context)
func NewHttpServer() *HttpServer {
// 实例化gin Server
router := gin.New()
router.Use(Recovery(), InjectFromRequest(), Metric(), Trace())
router.Use(Recovery(), AccessLog(), InjectFromRequest(), Metric(), Trace())

return &HttpServer{router}
}
Expand Down Expand Up @@ -170,7 +176,7 @@ func InjectFromRequest() gin.HandlerFunc {
func Metric() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.Request.Context()
ctx = contextWithErrCode(ctx,1)
ctx = contextWithErrCode(ctx, 1)
c.Request = c.Request.WithContext(ctx)

now := time.Now()
Expand Down Expand Up @@ -207,6 +213,33 @@ func Trace() gin.HandlerFunc {
}
}

func AccessLog() gin.HandlerFunc {
return func(c *gin.Context) {
// Start timer
st := xtime.NewTimeStat()
path := c.Request.URL.Path
bodyData, _ := c.GetRawData()
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyData))

c.Next()

method := c.Request.Method
statusCode := c.Writer.Status()
ctx := c.Request.Context()

keyAndValue := []interface{}{
"path", path,
"cost", st.Millisecond(),
"method", method,
"code", statusCode,
}
if shouldHttpLogRequest(path) {
keyAndValue = append(keyAndValue, "req", string(bodyData))
}
xlog.Infow(ctx, "", keyAndValue...)
}
}

// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
func Recovery() gin.HandlerFunc {
return func(c *gin.Context) {
Expand Down Expand Up @@ -366,3 +399,30 @@ func getErrCodeFromContext(ctx context.Context) int {
}
return errCode.int
}

func shouldHttpLogRequest(path string) bool {
// 默认打印body
center := GetConfigCenter()
if center == nil {
return true
}
printBodyMethod := printBodyMethod{}

// 方法配置
_ = center.Unmarshal(context.Background(), &printBodyMethod)
// 不打印的优先级更高
if methodInList(path, printBodyMethod.NoLogRequestMethodList) {
return false
}
if methodInList(path, printBodyMethod.LogRequestMethodList) {
return true
}

// 全局配置
isPrint, ok := center.GetBool(context.Background(), logRequestKey)
if !ok {
// 默认输出
return true
}
return isPrint
}

0 comments on commit 6a35f93

Please sign in to comment.