Skip to content

Commit

Permalink
fix: data race with multiple goroutine (#13)
Browse files Browse the repository at this point in the history
* fix: data race when using klog

* remove test
  • Loading branch information
hoanguyenkh authored Sep 23, 2024
1 parent 72bb05e commit 901d6db
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions klog/klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package klog

import (
"context"
"sync"

"github.com/KyberNetwork/logger"
)
Expand All @@ -12,7 +13,10 @@ func DefaultLogger() Logger {

type Logger = logger.Logger

var log Logger
var (
log Logger
once sync.Once
)

type Configuration struct {
EnableConsole bool
Expand All @@ -35,22 +39,26 @@ const (

func InitLogger(config Configuration, backend LoggerBackend) (Logger, error) {
var err error
log, err = logger.InitLogger(logger.Configuration{
EnableConsole: config.EnableConsole,
EnableJSONFormat: config.EnableJSONFormat,
ConsoleLevel: config.ConsoleLevel,
EnableFile: config.EnableFile,
FileJSONFormat: config.FileJSONFormat,
FileLevel: config.FileLevel,
FileLocation: config.FileLocation,
}, backend)
once.Do(func() {
log, err = logger.InitLogger(logger.Configuration{
EnableConsole: config.EnableConsole,
EnableJSONFormat: config.EnableJSONFormat,
ConsoleLevel: config.ConsoleLevel,
EnableFile: config.EnableFile,
FileJSONFormat: config.FileJSONFormat,
FileLevel: config.FileLevel,
FileLocation: config.FileLocation,
}, backend)
})
return log, err
}

func Log() Logger {
if log == nil {
log = DefaultLogger()
}
once.Do(func() {
if log == nil {
log = DefaultLogger()
}
})
return log
}

Expand Down

0 comments on commit 901d6db

Please sign in to comment.