generated from mrsimonemms/new
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
67 lines (54 loc) · 1.35 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
58
59
60
61
62
63
64
65
66
67
package ginstructuredlogger
import (
"time"
"github.com/gin-contrib/requestid"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const (
CtxKey = "logger"
)
func Get(c *gin.Context) *zerolog.Logger {
return c.MustGet(CtxKey).(*zerolog.Logger)
}
func New() gin.HandlerFunc {
return func(c *gin.Context) {
logCtx := (&log.Logger).With()
// Start timer
start := time.Now()
if requestId := requestid.Get(c); requestId != "" {
logCtx = logCtx.Str("requestID", requestId)
}
logger := logCtx.Logger()
// Store the logger so it can be used in contexts
c.Set(CtxKey, &logger)
// Continue to the next middleware
c.Next()
// Stop timer and calculate the latency
timeStamp := time.Now()
latency := timeStamp.Sub(start)
if latency > time.Minute {
latency = latency.Truncate(time.Second)
}
raw := c.Request.URL.RawQuery
path := c.Request.URL.Path
if raw != "" {
path = path + "?" + raw
}
var logEvent *zerolog.Event
if c.Writer.Status() >= 500 {
logEvent = logger.Error()
} else {
logEvent = logger.Debug()
}
logEvent.
Str("clientIP", c.ClientIP()).
Str("method", c.Request.Method).
Int("statusCode", c.Writer.Status()).
Int("bodySize", c.Writer.Size()).
Str("path", path).
Str("latency", latency.String()).
Msg(c.Errors.ByType(gin.ErrorTypePrivate).String())
}
}