-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
60 lines (45 loc) · 1.37 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package web
import "fmt"
import "time"
import "bytes"
import "encoding/json"
import "github.com/jackielihf/golib"
import "github.com/gin-gonic/gin"
var log = golib.Logger()
// middleware
func Logger(headers ...string) gin.HandlerFunc{
return func(c *gin.Context) {
// Start timer
start := time.Now()
// process request
c.Next()
log.Info(format(c, start, headers))
}
}
func format(c *gin.Context, start time.Time, headerKeys []string) string {
var b *bytes.Buffer = new(bytes.Buffer)
url := c.Request.URL.String()
end := time.Now()
latency := end.Sub(start)
clientIP := c.ClientIP()
method := c.Request.Method
statusCode := c.Writer.Status()
size := c.Writer.Size()
//header
userAgent := c.Request.UserAgent()
contentType := c.Request.Header.Get("content-type")
var headers = map[string]string{}
for _, key := range headerKeys {
value := c.Request.Header.Get(key)
headers[key] = value
}
headerString := ""
if len(headers) > 0 {
if headerBytes, err := json.Marshal(headers); err == nil {
headerString = string(headerBytes)
}
}
//to line
fmt.Fprintf(b, "%3d | %s %s | %13v | %dB | %s | %s | %s | %s", statusCode, method, url, latency, size, clientIP, contentType, userAgent, headerString)
return b.String()
}