Skip to content

Commit

Permalink
fix: initial error.
Browse files Browse the repository at this point in the history
  • Loading branch information
cauwulixuan committed Oct 14, 2022
1 parent 77bd2b7 commit e2ab245
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 47 deletions.
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ log:
http:
timeout: 30
retries:
enable: true
max_num_of_attempts: 3
max_backoff_delay: 5

Expand Down
34 changes: 1 addition & 33 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package config

import (
"fmt"
"github.com/spf13/pflag"
"log"
"os"
"path/filepath"
Expand All @@ -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))
}
Expand Down
10 changes: 4 additions & 6 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ var (
Client = resty.New()
)

func init() {
Init()
}

func Init() {
SetRetry()
func Init(retry bool) {
if retry {
SetRetry()
}
SetTimeout()
}

Expand Down
17 changes: 9 additions & 8 deletions log/zlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.")
}

Expand Down Expand Up @@ -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.")

}
Expand Down

0 comments on commit e2ab245

Please sign in to comment.