Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: data race with multiple goroutine #13

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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