generated from dzungtran/echo-rest-api
/
zap.go
146 lines (121 loc) · 3.05 KB
/
zap.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package logger
import (
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
log Logger
)
type (
CallLogOption struct {
applyFunc func(*logConfigs)
}
logConfigs struct {
Level zapcore.Level
// Encoding sets the logger's encoding. Valid values are "json" and "console"
Encoding string
}
Logger interface {
Infof(template string, args ...interface{})
Infow(msg string, keysAndValues ...interface{})
Info(args ...interface{})
Debugf(template string, args ...interface{})
Debugw(msg string, keysAndValues ...interface{})
Debug(args ...interface{})
Warnf(template string, args ...interface{})
Warnw(msg string, keysAndValues ...interface{})
Warn(args ...interface{})
Errorf(template string, args ...interface{})
Errorw(msg string, keysAndValues ...interface{})
Error(args ...interface{})
Panicf(template string, args ...interface{})
Panicw(msg string, keysAndValues ...interface{})
Panic(args ...interface{})
Fatalf(template string, args ...interface{})
Fatalw(msg string, keysAndValues ...interface{})
Fatal(args ...interface{})
Sync() error
}
)
func init() {
var cfg = zap.NewDevelopmentConfig()
cfg.EncoderConfig = zap.NewProductionEncoderConfig()
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
cfg.Encoding = "json"
cfg.OutputPaths = []string{"stdout"}
logger, _ := cfg.Build()
log = logger.Sugar()
}
// InitLog override default config
func InitLog(env string) {
if env == "production" {
var cfg = zap.NewProductionConfig()
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
cfg.Encoding = "json"
cfg.OutputPaths = []string{"stdout"}
cfg.Level.SetLevel(zap.WarnLevel)
logger, _ := cfg.Build()
log = logger.Sugar()
}
}
func Log() Logger {
return log
}
func Set(newLog Logger) {
log = newLog
}
func WithConfigLevel(level string) CallLogOption {
return CallLogOption{
applyFunc: func(oio *logConfigs) {
switch strings.ToLower(level) {
case "debug":
oio.Level = zapcore.DebugLevel
case "info":
oio.Level = zapcore.InfoLevel
case "warn":
oio.Level = zapcore.WarnLevel
case "error":
oio.Level = zapcore.ErrorLevel
case "fatal":
oio.Level = zapcore.FatalLevel
}
},
}
}
func WithConfigEncoding(encoding string) CallLogOption {
return CallLogOption{
applyFunc: func(oio *logConfigs) {
oio.Encoding = strings.ToLower(encoding)
},
}
}
func InitWithOptions(cnf ...CallLogOption) {
var cfg = zap.NewDevelopmentConfig()
cfg.EncoderConfig = zap.NewProductionEncoderConfig()
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
cfg.OutputPaths = []string{"stdout"}
cfg.Encoding = "json"
c := &logConfigs{}
if len(cnf) > 0 {
c = applyLogConfig(cnf)
}
if c.Encoding != "" {
cfg.Encoding = c.Encoding
}
if c.Level >= -1 {
cfg.Level.SetLevel(zap.DebugLevel)
}
logger, _ := cfg.Build()
log = logger.Sugar()
}
func applyLogConfig(callOptions []CallLogOption) *logConfigs {
if len(callOptions) == 0 {
return &logConfigs{}
}
optCopy := &logConfigs{}
for _, f := range callOptions {
f.applyFunc(optCopy)
}
return optCopy
}