-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.go
46 lines (39 loc) · 1.03 KB
/
core.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
package glog
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Core = zapcore.Core
type EncoderConfig = zapcore.EncoderConfig
type CoreConfig struct {
Encoding string `json:"encoding" yaml:"encoding"`
EncoderConfig EncoderConfig `json:"encoderConfig" yaml:"encoderConfig"`
OutputPaths []string `json:"outputPaths" yaml:"outputPaths"`
}
func (c *CoreConfig) buildEncoder() (zapcore.Encoder, error) {
switch c.Encoding {
case "json":
return zapcore.NewJSONEncoder(c.EncoderConfig), nil
case "console":
return zapcore.NewConsoleEncoder(c.EncoderConfig), nil
default:
return nil, fmt.Errorf("unsupported encoding: %s", c.Encoding)
}
}
func (c *CoreConfig) openSinks() (zapcore.WriteSyncer, error) {
sink, _, err := zap.Open(c.OutputPaths...)
return sink, err
}
func (c *CoreConfig) Build(lvl LevelEnabler) (core Core, err error) {
enc, err := c.buildEncoder()
if err != nil {
return
}
sink, err := c.openSinks()
if err != nil {
return
}
core = zapcore.NewCore(enc, sink, lvl)
return
}