-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
83 lines (68 loc) · 1.72 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
package kit
import (
"fmt"
"os"
"strings"
"go.uber.org/zap"
"github.com/spf13/viper"
)
func Read(env string, config interface{}) {
viper.SetConfigName("config")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
if env != "" {
f, err := os.Open("config." + env + ".yml")
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
defer f.Close()
viper.MergeConfig(f)
}
if err := viper.Unmarshal(config); err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
}
type LoggerConfig struct {
Level string
DisableCaller bool
DisableStacktrace bool
OutputPath string
ErrorOutputPath string
}
func (config LoggerConfig) Build() *zap.Logger {
var zapConfig zap.Config
switch strings.ToLower(config.Level) {
case "debug":
zapConfig = zap.NewDevelopmentConfig()
zapConfig.Level.SetLevel(zap.DebugLevel)
default:
zapConfig = zap.NewProductionConfig()
switch strings.ToLower(config.Level) {
case "info":
zapConfig.Level.SetLevel(zap.InfoLevel)
case "warn":
zapConfig.Level.SetLevel(zap.WarnLevel)
case "error":
zapConfig.Level.SetLevel(zap.ErrorLevel)
case "panic":
zapConfig.Level.SetLevel(zap.PanicLevel)
case "fatal":
zapConfig.Level.SetLevel(zap.FatalLevel)
}
}
if config.OutputPath != "" {
zapConfig.OutputPaths = []string{config.OutputPath}
}
if config.ErrorOutputPath != "" {
zapConfig.ErrorOutputPaths = []string{config.ErrorOutputPath}
}
zapConfig.DisableCaller = config.DisableCaller
zapConfig.DisableStacktrace = config.DisableStacktrace
logger, err := zapConfig.Build()
if err != nil {
panic(err)
}
return logger
}