/
logging.go
64 lines (52 loc) · 1.2 KB
/
logging.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
package logging
import (
"context"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
LogFormatJSON = "json"
LogFormatConsole = "console"
)
type Option func(*zap.Config)
func WithLogLevel(level string) Option {
return func(c *zap.Config) {
ll := zapcore.DebugLevel
_ = ll.Set(level)
c.Level.SetLevel(ll)
}
}
func WithLogFormat(format string) Option {
return func(c *zap.Config) {
switch format {
case LogFormatJSON:
c.Encoding = LogFormatJSON
case LogFormatConsole:
c.Encoding = LogFormatConsole
default:
c.Encoding = LogFormatJSON
}
}
}
func WithOutputPaths(paths []string) Option {
return func(c *zap.Config) {
c.OutputPaths = paths
}
}
// Init creates a new zap logger and attaches it to the provided context.
func Init(ctx context.Context, opts ...Option) (context.Context, error) {
zc := zap.NewProductionConfig()
zc.Sampling = nil
zc.DisableStacktrace = true
for _, opt := range opts {
opt(&zc)
}
l, err := zc.Build()
if err != nil {
return nil, err
}
zap.ReplaceGlobals(l)
l.Debug("Logger created!", zap.String("log_level", zc.Level.String()))
return ctxzap.ToContext(ctx, l), nil
}