-
Notifications
You must be signed in to change notification settings - Fork 5
/
logger.go
60 lines (51 loc) · 1.38 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
package azugo
import (
"go.uber.org/zap"
"golang.org/x/exp/maps"
)
func (ctx *Context) initLoggerFields() {
method := ctx.Method()
path := ctx.Path()
cleanedPath := path
basePath := ctx.BasePath()
if len(basePath) > 0 && len(basePath) < len(path) && basePath == path[:len(basePath)] {
cleanedPath = path[len(basePath):]
}
fields := make([]zap.Field, 0, 8)
fields = append(fields,
// Basic request fields
zap.String("http.request.id", ctx.ID()),
zap.String("http.request.method", method),
zap.String("url.path", cleanedPath),
// Source
zap.String("source.ip", ctx.IP().String()),
)
_ = ctx.AddLogFields(fields...)
}
// AddLogFields add fields to context logger.
func (ctx *Context) AddLogFields(fields ...zap.Field) error {
if len(fields) == 0 {
return nil
}
for _, field := range fields {
ctx.loggerFields[field.Key] = field
}
return ctx.ReplaceLogger(ctx.loggerCore)
}
// ReplaceLogger replaces current context logger with custom.
func (ctx *Context) ReplaceLogger(logger *zap.Logger) error {
if logger == nil {
return nil
}
ctx.loggerCore = logger
ctx.logger = logger.With(maps.Values(ctx.loggerFields)...)
return nil
}
// Log returns the logger.
func (ctx *Context) Log() *zap.Logger {
return ctx.logger
}
// SkipRequestLog sets to skip request log entry for current request.
func (ctx *Context) SkipRequestLog() {
ctx.SetUserValue("log_request", false)
}