在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
概述 - request 请求数据 - request_time - request_method - request_uri - request_proto - request_ua - request_referer - request_post_data - request_client_ip - response 返回数据 - response_time - response_code - response_msg - response_data - cost_time 花费时间
engine.Use(gin.Logger())
[GIN] 2019/08/30 - 21:24:16 | 200 | 178.072µs | ::1 | GET /ping [GIN] 2019/08/30 - 21:24:27 | 200 | 367.997µs | ::1 | POST /product [GIN] 2019/08/30 - 21:24:28 | 200 | 2.521592ms | ::1 | POST /product
f, _ := os.Create(config.AppAccessLogName) gin.DefaultWriter = io.MultiWriter(f) engine.Use(gin.Logger())
[GIN] 2019/08/30 - 21:36:07 | 200 | 369.023µs | ::1 | GET /ping [GIN] 2019/08/30 - 21:36:08 | 200 | 27.585µs | ::1 | GET /ping [GIN] 2019/08/30 - 21:36:10 | 200 | 14.302µs | ::1 | POST /product
middleware/logger/logger.go package logger import ( "bytes" "encoding/json" "fmt" "github.com/gin-gonic/gin" "go-gin-api/app/config" "go-gin-api/app/util" "log" "os" ) type bodyLogWriter struct { gin.ResponseWriter body *bytes.Buffer } func (w bodyLogWriter) Write(b []byte) (int, error) { w.body.Write(b) return w.ResponseWriter.Write(b) } func (w bodyLogWriter) WriteString(s string) (int, error) { w.body.WriteString(s) return w.ResponseWriter.WriteString(s) } func SetUp() gin.HandlerFunc { return func(c *gin.Context) { bodyLogWriter := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer} c.Writer = bodyLogWriter //开始时间 startTime := util.GetCurrentMilliTime() //处理请求 c.Next() responseBody := bodyLogWriter.body.String() var responseCode int var responseMsg string var responseData interface{} if responseBody != "" { response := util.Response{} err := json.Unmarshal([]byte(responseBody), &response) if err == nil { responseCode = response.Code responseMsg = response.Message responseData = response.Data } } //结束时间 endTime := util.GetCurrentMilliTime() if c.Request.Method == "POST" { c.Request.ParseForm() } //日志格式 accessLogMap := make(map[string]interface{}) accessLogMap["request_time"] = startTime accessLogMap["request_method"] = c.Request.Method accessLogMap["request_uri"] = c.Request.RequestURI accessLogMap["request_proto"] = c.Request.Proto accessLogMap["request_ua"] = c.Request.UserAgent() accessLogMap["request_referer"] = c.Request.Referer() accessLogMap["request_post_data"] = c.Request.PostForm.Encode() accessLogMap["request_client_ip"] = c.ClientIP() accessLogMap["response_time"] = endTime accessLogMap["response_code"] = responseCode accessLogMap["response_msg"] = responseMsg accessLogMap["response_data"] = responseData accessLogMap["cost_time"] = fmt.Sprintf("%vms", endTime - startTime) accessLogJson, _ := util.JsonEncode(accessLogMap) if f, err := os.OpenFile(config.AppAccessLogName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666); err != nil { log.Println(err) } else { f.WriteString(accessLogJson + "\n") } } }
{"cost_time":"0ms","request_client_ip":"::1","request_method":"GET","request_post_data":"","request_proto":"HTTP/1.1","request_referer":"","request_time":1567172568233,"request_ua":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36","request_uri":"/ping","response_code":1,"response_data":null,"response_msg":"pong","response_time":1567172568233} {"cost_time":"0ms","request_client_ip":"::1","request_method":"GET","request_post_data":"","request_proto":"HTTP/1.1","request_referer":"","request_time":1567172569158,"request_ua":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36","request_uri":"/ping","response_code":1,"response_data":null,"response_msg":"pong","response_time":1567172569158} {"cost_time":"0ms","request_client_ip":"::1","request_method":"POST","request_post_data":"name=admin","request_proto":"HTTP/1.1","request_referer":"","request_time":1567172629565,"request_ua":"PostmanRuntime/7.6.0","request_uri":"/product","response_code":-1,"response_data":null,"response_msg":"Key: 'ProductAdd.Name' Error:Field validation for 'Name' failed on the 'NameValid' tag","response_time":1567172629565}
链接:https://pan.baidu.com/s/1v5gm7n0L7TGyejCmQrMh2g 提取码:x2p5 |
请发表评论