-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
51 lines (44 loc) · 1.26 KB
/
middleware.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
package logger
import (
"fmt"
"time"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
// GinLogger returns a new Gin middleware that performs logging for our JSON APIs using
// zerolog rather than the default Gin logger which is a standard HTTP logger.
// NOTE: we previously used github.com/dn365/gin-zerolog but wanted more customization.
func GinLogger(server string) gin.HandlerFunc {
return func(c *gin.Context) {
// Before request
started := time.Now()
path := c.Request.URL.Path
if c.Request.URL.RawQuery != "" {
path = path + "?" + c.Request.URL.RawQuery
}
// Handle the request
c.Next()
// After request
status := c.Writer.Status()
logctx := log.With().
Strs("errors", c.Errors.Errors()).
Str("path", path).
Str("ser_name", server).
Str("method", c.Request.Method).
Dur("resp_time", time.Since(started)).
Int("resp_bytes", c.Writer.Size()).
Int("status", status).
Str("client_ip", c.ClientIP()).
Str("request_id", c.GetString("request_id")).
Logger()
msg := fmt.Sprintf("%s %s %s %d", server, c.Request.Method, c.Request.URL.Path, status)
switch {
case status >= 400 && status < 500:
logctx.Warn().Msg(msg)
case status >= 500:
logctx.Error().Msg(msg)
default:
logctx.Info().Msg(msg)
}
}
}