-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
48 lines (38 loc) · 951 Bytes
/
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
package zap
import (
"time"
"github.com/alexfalkowski/go-service/env"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// IsEnabled for zap.
func IsEnabled(cfg *Config) bool {
return cfg != nil
}
// Config for zap.
type Config struct {
Level string `yaml:"level,omitempty" json:"level,omitempty" toml:"level,omitempty"`
}
// NewConfig for zap.
func NewConfig(env env.Environment, config *Config) (zap.Config, error) {
if !IsEnabled(config) {
return zap.Config{}, nil
}
l, err := zap.ParseAtomicLevel(config.Level)
if err != nil {
return zap.Config{}, err
}
var cfg zap.Config
if env.IsDevelopment() {
cfg = zap.NewDevelopmentConfig()
} else {
cfg = zap.NewProductionConfig()
}
cfg.Level = l
cfg.DisableCaller = true
cfg.DisableStacktrace = true
cfg.EncoderConfig.EncodeTime = zapcore.TimeEncoder(func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format(time.RFC3339))
})
return cfg, nil
}