Skip to content

Commit

Permalink
*: fix cmdLogger style
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Dec 8, 2023
1 parent 4920739 commit d172d20
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
9 changes: 7 additions & 2 deletions cmd/ehco/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,6 @@ func initLogger(cfg *config.Config) error {
}

func start(ctx *cli.Context) error {
cmdLogger = zap.NewExample().Sugar().Named("cmd")

cfg, err := loadConfig()
if err != nil {
return err
Expand Down Expand Up @@ -432,6 +430,13 @@ func main() {
}
}()

l, err := log.NewLogger("info")
if err != nil {
println("init logger failed,err=", err)
os.Exit(2)
}
cmdLogger = l.Sugar()

app := createCliAPP()
// register start command
app.Action = start
Expand Down
12 changes: 8 additions & 4 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (

var doOnce sync.Once

func initLogger(logLevel string) error {
func initLogger(logLevel string, replaceGlobal bool) (*zap.Logger, error) {
level := zapcore.InfoLevel
if err := level.UnmarshalText([]byte(logLevel)); err != nil {
return err
return nil, err
}
writers := []zapcore.WriteSyncer{zapcore.AddSync(os.Stdout)}
encoder := zapcore.EncoderConfig{
Expand All @@ -32,13 +32,17 @@ func initLogger(logLevel string) error {
)
l := zap.New(core)
zap.ReplaceGlobals(l)
return nil
return l, nil
}

func InitGlobalLogger(logLevel string) error {
var err error
doOnce.Do(func() {
err = initLogger(logLevel)
_, err = initLogger(logLevel, true)
})
return err
}

func NewLogger(logLevel string) (*zap.Logger, error) {
return initLogger(logLevel, false)
}

0 comments on commit d172d20

Please sign in to comment.