-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
142 lines (124 loc) · 3.65 KB
/
config.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
package glog
import (
"github.com/ace-zhaoy/glog/cores"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"sort"
"time"
)
type SamplingConfig = zap.SamplingConfig
type Config struct {
Name string `json:"name" yaml:"name"`
Level Level `json:"level" yaml:"level"`
LazyDisabled bool `json:"lazyDisabled" yaml:"lazyDisabled"`
AddCaller bool `json:"addCaller" yaml:"addCaller"`
StackLevel *Level `json:"stackLevel" yaml:"stackLevel"`
CallerSkip int `json:"callerSkip" yaml:"callerSkip"`
FormatEnabled bool `json:"formatEnabled" yaml:"formatEnabled"`
ContextFields map[string]string `json:"contextFields" yaml:"contextFields"`
Sampling *SamplingConfig `json:"sampling" yaml:"sampling"`
InitialFields map[string]any `json:"initialFields" yaml:"initialFields"`
Core CoreConfig `json:"core" yaml:"core"`
}
func (c *Config) buildOptions() []Option {
opts := make([]Option, 0, 10)
if c.Name != "" {
opts = append(opts, WithName(c.Name))
}
if c.LazyDisabled {
opts = append(opts, WrapCore(func(core Core) Core {
return cores.NewLazyCore(core)
}))
}
if c.AddCaller {
opts = append(opts, AddCaller())
}
if c.StackLevel != nil {
opts = append(opts, WithStack(c.StackLevel))
}
if c.CallerSkip != 0 {
opts = append(opts, WithCallerSkip(c.CallerSkip))
}
if c.FormatEnabled {
opts = append(opts, WithFormatEnabled())
}
if len(c.ContextFields) > 0 {
keys := make([]string, 0, len(c.ContextFields))
for k := range c.ContextFields {
keys = append(keys, k)
}
sort.Strings(keys)
contextHandlers := make([]ContextHandler, 0, len(c.ContextFields))
for _, k := range keys {
contextHandlers = append(contextHandlers, BuildContextHandler(k, c.ContextFields[k]))
}
opts = append(opts, WithContextHandlers(contextHandlers...))
}
if len(c.InitialFields) > 0 {
keys := make([]string, 0, len(c.InitialFields))
for k := range c.InitialFields {
keys = append(keys, k)
}
sort.Strings(keys)
fds := make([]Field, 0, len(c.InitialFields))
for _, k := range keys {
fds = append(fds, Any(k, c.InitialFields[k]))
}
opts = append(opts, WrapCore(func(core Core) Core {
return core.With(fds)
}))
}
if c.Sampling != nil {
opts = append(opts, WrapCore(func(core zapcore.Core) zapcore.Core {
var samplerOpts []zapcore.SamplerOption
if c.Sampling.Hook != nil {
samplerOpts = append(samplerOpts, zapcore.SamplerHook(c.Sampling.Hook))
}
return zapcore.NewSamplerWithOptions(
core,
time.Second,
c.Sampling.Initial,
c.Sampling.Thereafter,
samplerOpts...,
)
}))
}
return opts
}
func (c *Config) Build(opts ...Option) (*Logger, error) {
core, err := c.Core.Build(c.Level)
if err != nil {
return nil, err
}
return NewLogger(core, c.buildOptions()...).WithOptions(opts...), nil
}
func NewDefaultConfig() *Config {
stackLevel := LevelError
return &Config{
Level: LevelDebug,
AddCaller: true,
StackLevel: &stackLevel,
Sampling: &SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Core: CoreConfig{
Encoding: "json",
EncoderConfig: EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.RFC3339NanoTimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{"stderr"},
},
}
}