github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/ginx/hlog/log.go (about) 1 package hlog 2 3 import ( 4 "net/http" 5 "time" 6 7 "github.com/gin-gonic/gin" 8 ) 9 10 // Store defines the interface to Store a log. 11 type Store interface { 12 // Store stores the log in database like MySQL, InfluxDB, and etc. 13 Store(c *gin.Context, log *Log) 14 } 15 16 // Log describes info about HTTP request. 17 type Log struct { 18 ID string 19 Biz string 20 21 // Method is GET etc. 22 Method string 23 URL string 24 IPAddr string 25 26 RspHeader http.Header 27 ReqBody string 28 29 // RspStatus, like 200, 404. 30 RspStatus int 31 // ReqHeader records the response header. 32 ReqHeader http.Header 33 // RespSize is number of bytes of the response sent. 34 RespSize int 35 // RspBody is the response body(limit to 1000). 36 RspBody string 37 38 Created time.Time 39 40 // Start records the start time of the request. 41 Start time.Time 42 // End records the end time of the request. 43 End time.Time 44 // Duration means how long did it take to. 45 Duration time.Duration 46 Attrs Attrs 47 48 Option *Option 49 PathParams gin.Params 50 Request *http.Request 51 } 52 53 func (l *Log) pathVar(name string) string { 54 for _, p := range l.PathParams { 55 if p.Key == name { 56 return p.Value 57 } 58 } 59 60 return "" 61 } 62 63 func (l *Log) pathVars() interface{} { 64 m := make(map[string]string) 65 66 for _, p := range l.PathParams { 67 m[p.Key] = p.Value 68 } 69 70 return m 71 } 72 73 func (l *Log) queryVar(name string) string { 74 return At(l.Request.URL.Query()[name], 0) 75 } 76 77 func (l *Log) queryVars() string { 78 return l.Request.URL.Query().Encode() 79 } 80 81 func (l *Log) paramVar(name string) string { 82 return At(l.Request.Form[name], 0) 83 } 84 85 func (l *Log) paramVars() string { 86 return l.Request.Form.Encode() 87 }