diff --git a/config.yaml b/config.yaml index 73cd6f9..8307844 100644 --- a/config.yaml +++ b/config.yaml @@ -26,6 +26,7 @@ log: http: timeout: 30 retries: + enable: true max_num_of_attempts: 3 max_backoff_delay: 5 diff --git a/config/config.go b/config/config.go index 657490a..347e91a 100644 --- a/config/config.go +++ b/config/config.go @@ -20,7 +20,6 @@ package config import ( "fmt" - "github.com/spf13/pflag" "log" "os" "path/filepath" @@ -43,39 +42,8 @@ func ValidateConfigPath(path string) error { return nil } -// ParseFlags will create and parse the CLI flags -// and return the path to be used elsewhere -func ParseFlags() (string, error) { - // String that contains the configured configuration path - var configPath string - - // Set up a CLI flag called "--config" or "-c" to allow users - // to supply the configuration file - pflag.StringVarP(&configPath, "config", "c", "./config.yaml", "path to config file") - - // Actually parse the flags - pflag.Parse() - - // Validate the path first - if err := ValidateConfigPath(configPath); err != nil { - return "", err - } - - // Return the configuration path - return configPath, nil -} - -// init initial config while app start -func init() { - Init() -} - // Init initial config and watch config on change -func Init() { - cfgPath, err := ParseFlags() - if err != nil { - panic(fmt.Errorf("fatal error config file: %v", err)) - } +func Init(cfgPath string) { if err := initConfig(cfgPath); err != nil { panic(fmt.Errorf("Init config failed, error: %v", err)) } diff --git a/http/client.go b/http/client.go index a75d904..fb39772 100644 --- a/http/client.go +++ b/http/client.go @@ -36,12 +36,10 @@ var ( Client = resty.New() ) -func init() { - Init() -} - -func Init() { - SetRetry() +func Init(retry bool) { + if retry { + SetRetry() + } SetTimeout() } diff --git a/log/zlog.go b/log/zlog.go index 73a49c1..6fa3771 100644 --- a/log/zlog.go +++ b/log/zlog.go @@ -53,12 +53,7 @@ func getLogLevel(level string) zapcore.Level { } } -func init() { - Init() -} - -func Init() { - multi := viper.GetBool("log.multi_staging") +func Init(multi bool) { if multi { InitWithMultiLevelOutPut() } else { @@ -79,11 +74,14 @@ func InitWithSingleLevelOutput() { // 2. AddStacktrace record a stack trace for all messages at or above WARN level. // 3. Add serviceName field. field := zap.Fields(zap.String("serviceName", viper.GetString("svc_name"))) - logger = zap.New(core, zap.AddCaller(), zap.AddStacktrace(zap.LevelEnablerFunc(warnLevel)), field) + + // zap.AddCallerSkip(1) skip wrapper function. + logger = zap.New(core, zap.AddCaller(), zap.AddStacktrace(zap.LevelEnablerFunc(warnLevel)), zap.AddCallerSkip(1), field) defer logger.Sync() zap.ReplaceGlobals(logger) Slogger = logger.Sugar() + defer Slogger.Sync() Slogger.Info("Setting logger successfully.") } @@ -116,11 +114,14 @@ func InitWithMultiLevelOutPut() { // 2. AddStacktrace record a stack trace for all messages at or above WARN level. // 3. Add serviceName field. field := zap.Fields(zap.String("serviceName", viper.GetString("svc_name"))) - logger = zap.New(core, zap.AddCaller(), zap.AddStacktrace(warnLvl), field) + + // zap.AddCallerSkip(1) skip wrapper function. + logger = zap.New(core, zap.AddCaller(), zap.AddStacktrace(warnLvl), zap.AddCallerSkip(1), field) defer logger.Sync() zap.ReplaceGlobals(logger) Slogger = logger.Sugar() + defer Slogger.Sync() Slogger.Info("Setting logger successfully.") }