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
86
87
88
89
90
91
92
93
94
95
|
package logging
import (
"context"
"sync"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type contextKey = string
const loggerKey = contextKey("logger")
var (
defaultLogger *zap.SugaredLogger
defaultLoggerOnce sync.Once
)
var conf = &Config{
Encoding: "console",
Level: zapcore.InfoLevel,
Development: true,
}
type Config struct {
Encoding string
Level zapcore.Level
Development bool
}
// SetConfig sets given logging configs for DefaultLogger's logger.
// Must set configs before calling DefaultLogger()
func SetConfig(c *Config) {
conf = &Config{
Encoding: c.Encoding,
Level: c.Level,
Development: c.Development,
}
}
func SetLevel(l zapcore.Level) {
conf.Level = l
}
// NewLogger creates a new logger with the given log level
func NewLogger(conf *Config) *zap.SugaredLogger {
ec := zap.NewProductionEncoderConfig()
ec.EncodeTime = zapcore.ISO8601TimeEncoder
cfg := zap.Config{
Encoding: conf.Encoding,
EncoderConfig: ec,
Level: zap.NewAtomicLevelAt(conf.Level),
Development: conf.Development,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
logger, err := cfg.Build()
if err != nil {
logger = zap.NewNop()
}
return logger.Sugar()
}
// DefaultLogger returns the default logger for the package.
func DefaultLogger() *zap.SugaredLogger {
defaultLoggerOnce.Do(func() {
defaultLogger = NewLogger(conf)
})
return defaultLogger
}
// WithLogger creates a new context with the provided logger attached.
func WithLogger(ctx context.Context, logger *zap.SugaredLogger) context.Context {
if gCtx, ok := ctx.(*gin.Context); ok {
ctx = gCtx.Request.Context()
}
return context.WithValue(ctx, loggerKey, logger)
}
// FromContext returns the logger stored in the context. If no such logger
// exists, a default logger is returned.
func FromContext(ctx context.Context) *zap.SugaredLogger {
if ctx == nil {
return DefaultLogger()
}
if gCtx, ok := ctx.(*gin.Context); ok && gCtx != nil {
ctx = gCtx.Request.Context()
}
if logger, ok := ctx.Value(loggerKey).(*zap.SugaredLogger); ok {
return logger
}
return DefaultLogger()
}
|