/
logger.go
57 lines (47 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
package middleware
import (
"fmt"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"time"
)
const LoggerKey = "l"
func GetLoggerFromCtx(ctx *gin.Context) *zap.Logger {
l, ok := ctx.Get(LoggerKey)
if !ok {
panic("could not access logger")
}
return l.(*zap.Logger)
}
func GoodLoggerMiddleware(logger *zap.Logger) gin.HandlerFunc {
return func(ctx *gin.Context) {
start := time.Now()
// some evil middlewares modify this values
path := ctx.Request.URL.Path
query := ctx.Request.URL.RawQuery
fields := []zapcore.Field{
zap.String("method", ctx.Request.Method),
zap.String("path", path),
zap.String("query", query),
zap.String("ip", ctx.ClientIP()),
zap.String("user-agent", ctx.Request.UserAgent()),
}
subLogger := logger.With(fields...)
ctx.Set(LoggerKey, subLogger)
ctx.Next() // <- execute next thing in the chain
end := time.Now()
latency := end.Sub(start)
completedRequestFields := []zapcore.Field{
zap.Int("status", ctx.Writer.Status()),
zap.Duration("latency", latency),
}
if len(ctx.Errors) > 0 {
// Append error field if this is an erroneous request.
for _, e := range ctx.Errors.Errors() {
subLogger.Error(e, completedRequestFields...)
}
}
subLogger.Debug(fmt.Sprintf("%s %s served: %d", ctx.Request.Method, path, ctx.Writer.Status()), completedRequestFields...)
}
}