-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathlogger.go
More file actions
65 lines (51 loc) · 1.42 KB
/
logger.go
File metadata and controls
65 lines (51 loc) · 1.42 KB
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
58
59
60
61
62
63
64
65
package middleware
import (
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/netless-io/flat-server/logger"
"go.uber.org/zap"
)
type PayLoad struct {
DurationMS int64 `json:"duration_ms"`
Method string `json:"method"`
StatusCode int `json:"status_code"`
BodySize int `json:"body_size"`
User *UserPayLoad `json:"user,omitempty"`
}
type UserPayLoad struct {
UserID string `json:"user_id,omitempty"`
LoginSource string `json:"login_source,omitempty"`
IAT int64 `json:"iat,omitempty"`
EXP int64 `json:"exp,omitempty"`
}
func Logger() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
requestID := strings.ReplaceAll(uuid.New().String(), "-", "")
c.Set("request_id", requestID)
// Process request
c.Next()
bodySize := c.Writer.Size()
if bodySize < 0 {
bodySize = 0
}
payload := PayLoad{
DurationMS: time.Since(start).Milliseconds(),
Method: c.Request.Method,
BodySize: bodySize,
StatusCode: c.Writer.Status(),
}
// TODO jwt trace log
if userAuth, exists := c.Get("user_auth"); exists {
if userPayLoad, ok := userAuth.(UserPayLoad); ok {
payload.User = &userPayLoad
}
}
param := make(map[string]PayLoad)
param[path] = payload
logger.Infow("router info", zap.String("request_id", requestID), zap.Any("payload", param))
}
}