-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
85 lines (72 loc) · 1.99 KB
/
log.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Copyright FMR LLC <opensource@fidelity.com>
*
* SPDX-License-Identifier: Apache
*/
package logging
import (
"context"
"net/http"
"github.com/go-chi/chi/v5/middleware"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var DefaultLogger *zap.Logger
// Build a *zap.Logger from zap.Config
func getLogger(config zap.Config) *zap.Logger {
loggerMgr, err := config.Build()
if err != nil {
panic("Error building Zap logger")
}
return loggerMgr
}
// Return a new *zap.Logger instance, accept a zap.Config
func NewDefaultLogger(config zap.Config) *zap.Logger {
loggerMgr := getLogger(config)
DefaultLogger = loggerMgr
return loggerMgr
}
// default zap.Config. accept level of int type, vaule between -1 and 5
func DefaultLogConfig(level int) zap.Config {
logConfig := zap.NewProductionConfig()
logConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
if level < -1 || level > 5 {
logConfig.Level.SetLevel(zapcore.InfoLevel)
} else {
logConfig.Level.SetLevel(zapcore.Level(level))
}
return logConfig
}
// Return default Logger instance.
func L() *zap.Logger {
if DefaultLogger == nil {
NewDefaultLogger(DefaultLogConfig(0))
}
return DefaultLogger
}
// Return default Logger.Sugar instance.
func S() *zap.SugaredLogger {
return L().Sugar()
}
// Return Logger instance with request id from context
func LWithContext(ctx context.Context) *zap.Logger {
l := L()
if reqID := middleware.GetReqID(ctx); reqID != "" {
l = DefaultLogger.With(zap.String("RequestId", reqID))
}
return l
}
// Return Logger.Sugar instance with request id from context
func SWithContext(ctx context.Context) *zap.SugaredLogger {
return LWithContext(ctx).Sugar()
}
// Used as middleware, to insert request id into the response header if present
func RequestIDHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if reqID := middleware.GetReqID(r.Context()); reqID != "" {
w.Header().Set("X-Request-Id", reqID)
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}