-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
94 lines (78 loc) · 2.27 KB
/
root.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
package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/uwblueprint/shoe-project/config"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
configFile string
// root CLI command
rootCmd = &cobra.Command{}
)
// init initializes config, and logs
func init() {
cobra.OnInitialize(initConfig, initLogger)
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "Config file for the server")
}
// initConfig reads in config file provided and ENV variables if set
func initConfig() {
viper.SetTypeByDefaultValue(true)
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
if configFile != "" {
viper.SetConfigFile(configFile)
if err := viper.ReadInConfig(); err != nil {
fmt.Fprintf(os.Stderr, "Could not read config file %s: %v\n", configFile, err.Error())
os.Exit(1)
}
}
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
}
}
// initLogger sets up logger based on configuration provided
func initLogger() {
var logConfig zap.Config
if config.GetMode() == config.MODE_PROD {
logConfig = zap.NewProductionConfig()
} else {
logConfig = zap.NewDevelopmentConfig()
}
var logLevel zapcore.Level
if err := logLevel.Set(config.GetLoggerLevel()); err != nil {
fmt.Fprintf(os.Stderr, "Logger level setup error: %v\n", err)
os.Exit(1)
}
logConfig.Level.SetLevel(logLevel)
// Settings
logConfig.Encoding = config.GetLoggerEncoding()
logConfig.Development = config.GetMode() == config.MODE_DEV
logConfig.DisableCaller = false
logConfig.DisableStacktrace = config.IsLoggerStacktraceDisabled()
// Enable Color
if config.IsLoggerColored() {
logConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
// Use sane timestamp when logging to console
if logConfig.Encoding == config.LOGGER_ENCODING_CONSOLE {
logConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
}
// JSON Fields
logConfig.EncoderConfig.MessageKey = "msg"
logConfig.EncoderConfig.LevelKey = "level"
logConfig.EncoderConfig.CallerKey = "caller"
// Build the logger
globalLogger, err := logConfig.Build()
if err != nil {
fmt.Fprintf(os.Stderr, "Logger build error: %v\n", err)
os.Exit(1)
}
zap.ReplaceGlobals(globalLogger)
}