From 9cd72429cddbfd402e891d058a22c23abc52a7eb Mon Sep 17 00:00:00 2001 From: manke lu Date: Fri, 7 Jun 2024 04:16:23 +0800 Subject: [PATCH 1/2] Fix InitLogger just no work --- clients/config_client/config_client.go | 8 +++++++- clients/naming_client/naming_client.go | 9 ++++++++- common/logger/logger.go | 3 --- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/clients/config_client/config_client.go b/clients/config_client/config_client.go index 22b49b61..51f7f5d5 100644 --- a/clients/config_client/config_client.go +++ b/clients/config_client/config_client.go @@ -46,6 +46,8 @@ const ( executorErrDelay = 5 * time.Second ) +var onceInitLogger sync.Once + type ConfigClient struct { ctx context.Context cancel context.CancelFunc @@ -153,7 +155,11 @@ func NewConfigClient(nc nacos_client.INacosClient) (*ConfigClient, error) { } func initLogger(clientConfig constant.ClientConfig) error { - return logger.InitLogger(logger.BuildLoggerConfig(clientConfig)) + var err error + onceInitLogger.Do(func() { + err = logger.InitLogger(logger.BuildLoggerConfig(clientConfig)) + }) + return err } func (client *ConfigClient) GetConfig(param vo.ConfigParam) (content string, err error) { diff --git a/clients/naming_client/naming_client.go b/clients/naming_client/naming_client.go index a2257e1a..a7a507fd 100644 --- a/clients/naming_client/naming_client.go +++ b/clients/naming_client/naming_client.go @@ -21,6 +21,7 @@ import ( "math" "math/rand" "strings" + "sync" "time" "github.com/pkg/errors" @@ -35,6 +36,8 @@ import ( "github.com/nacos-group/nacos-sdk-go/v2/vo" ) +var onceInitLogger sync.Once + // NamingClient ... type NamingClient struct { nacos_client.INacosClient @@ -88,7 +91,11 @@ func NewNamingClient(nc nacos_client.INacosClient) (*NamingClient, error) { } func initLogger(clientConfig constant.ClientConfig) error { - return logger.InitLogger(logger.BuildLoggerConfig(clientConfig)) + var err error + onceInitLogger.Do(func() { + err = logger.InitLogger(logger.BuildLoggerConfig(clientConfig)) + }) + return err } // RegisterInstance ... diff --git a/common/logger/logger.go b/common/logger/logger.go index b154fefd..f0d51101 100644 --- a/common/logger/logger.go +++ b/common/logger/logger.go @@ -118,9 +118,6 @@ func BuildLoggerConfig(clientConfig constant.ClientConfig) Config { func InitLogger(config Config) (err error) { logLock.Lock() defer logLock.Unlock() - if logger != nil { - return - } logger, err = InitNacosLogger(config) return } From a5c9ad651834429f545a648173fe03d52e59f18f Mon Sep 17 00:00:00 2001 From: manke Date: Wed, 12 Jun 2024 10:14:19 +0800 Subject: [PATCH 2/2] Delete once --- clients/config_client/config_client.go | 8 +------- clients/naming_client/naming_client.go | 9 +-------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/clients/config_client/config_client.go b/clients/config_client/config_client.go index 51f7f5d5..22b49b61 100644 --- a/clients/config_client/config_client.go +++ b/clients/config_client/config_client.go @@ -46,8 +46,6 @@ const ( executorErrDelay = 5 * time.Second ) -var onceInitLogger sync.Once - type ConfigClient struct { ctx context.Context cancel context.CancelFunc @@ -155,11 +153,7 @@ func NewConfigClient(nc nacos_client.INacosClient) (*ConfigClient, error) { } func initLogger(clientConfig constant.ClientConfig) error { - var err error - onceInitLogger.Do(func() { - err = logger.InitLogger(logger.BuildLoggerConfig(clientConfig)) - }) - return err + return logger.InitLogger(logger.BuildLoggerConfig(clientConfig)) } func (client *ConfigClient) GetConfig(param vo.ConfigParam) (content string, err error) { diff --git a/clients/naming_client/naming_client.go b/clients/naming_client/naming_client.go index a7a507fd..a2257e1a 100644 --- a/clients/naming_client/naming_client.go +++ b/clients/naming_client/naming_client.go @@ -21,7 +21,6 @@ import ( "math" "math/rand" "strings" - "sync" "time" "github.com/pkg/errors" @@ -36,8 +35,6 @@ import ( "github.com/nacos-group/nacos-sdk-go/v2/vo" ) -var onceInitLogger sync.Once - // NamingClient ... type NamingClient struct { nacos_client.INacosClient @@ -91,11 +88,7 @@ func NewNamingClient(nc nacos_client.INacosClient) (*NamingClient, error) { } func initLogger(clientConfig constant.ClientConfig) error { - var err error - onceInitLogger.Do(func() { - err = logger.InitLogger(logger.BuildLoggerConfig(clientConfig)) - }) - return err + return logger.InitLogger(logger.BuildLoggerConfig(clientConfig)) } // RegisterInstance ...