-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.go
85 lines (73 loc) · 1.88 KB
/
util.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
package main
import (
"os"
"time"
mapset "github.com/deckarep/golang-set/v2"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v3"
)
const (
appTimeFormat = "2006-01-02T15:04:05Z0700"
)
type appLoggerType = *zap.SugaredLogger
func readConfig(filename string) ([]byte, error) {
data, err := os.ReadFile(filename)
if err != nil {
return []byte{}, errors.Wrapf(err, "failed to read file %s", filename)
}
return data, err
}
func unmarshallConfig(content []byte) (*Config, error) {
cfg := &Config{}
err := yaml.Unmarshal(content, cfg)
if err != nil {
return nil, errors.Wrapf(err, "failed to unmarshall config file: %s", content)
}
return cfg, nil
}
func loadConfig(filename string) (*Config, error) {
content, err := readConfig(filename)
if err != nil {
return nil, err
}
return unmarshallConfig(content)
}
func configureLogger(cfg *LoggingConfig, opts ...zap.Option) (*zap.SugaredLogger, error) {
var zapConfig zap.Config
if cfg.IsProduction {
zapConfig = zap.NewProductionConfig()
zapConfig.Sampling = nil // disable logs sampling, we don't have that much
zapConfig.EncoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.UTC().Format(appTimeFormat))
// 2019-08-13T04:39:11Z
}
} else {
zapConfig = zap.NewDevelopmentConfig()
}
zapLevel, err := zapcore.ParseLevel(cfg.Level)
if err != nil {
return nil, err
}
zapConfig.Level.SetLevel(zapLevel)
logger, err := zapConfig.Build(opts...)
if err != nil {
return nil, err
}
return logger.Sugar(), nil
}
func getDevelopmentLogger() appLoggerType {
return zap.Must(zap.NewDevelopment()).Sugar()
}
type StringSet mapset.Set[string]
func NewStringSet() StringSet {
return mapset.NewSet[string]()
}
func NewStringSetFromItems(slice ...string) StringSet {
set := NewStringSet()
for _, item := range slice {
set.Add(item)
}
return set
}