Skip to content

Commit

Permalink
bugfix dubbo-go issue: nacos-go-sdk stdout redirect #1960 (#489)
Browse files Browse the repository at this point in the history
fix:490
Co-authored-by: chenzf72 <chenzf72@chinaunicom.cn>
  • Loading branch information
chenzfp and chenzf72 authored Jul 17, 2022
1 parent 24e67a3 commit c7dfb75
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions clients/config_client/config_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func NewConfigClient(nc nacos_client.INacosClient) (*ConfigClient, error) {
LogRollingConfig: clientConfig.LogRollingConfig,
LogDir: clientConfig.LogDir,
CustomLogger: clientConfig.CustomLogger,
LogStdout: clientConfig.LogStdout,
}
err = logger.InitLogger(loggerConfig)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions clients/naming_client/naming_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func NewNamingClient(nc nacos_client.INacosClient) (NamingClient, error) {
LogRollingConfig: clientConfig.LogRollingConfig,
LogDir: clientConfig.LogDir,
CustomLogger: clientConfig.CustomLogger,
LogStdout: clientConfig.LogStdout,
}
err = logger.InitLogger(loggerConfig)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions common/constant/client_config_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,10 @@ func WithLogRollingConfig(rollingConfig *lumberjack.Logger) ClientOption {
config.LogRollingConfig = rollingConfig
}
}

// WithLogStdout ...
func WithLogStdout(logStdout bool) ClientOption {
return func(config *ClientConfig) {
config.LogStdout = logStdout
}
}
1 change: 1 addition & 0 deletions common/constant/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ type ClientConfig struct {
ContextPath string // the nacos server contextpath
LogRollingConfig *lumberjack.Logger // the log rolling config
CustomLogger logger.Logger // the custom log interface ,With a custom Logger (nacos sdk will not provide log cutting and archiving capabilities)
LogStdout bool // the stdout redirect for log, default is false
}
6 changes: 5 additions & 1 deletion common/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Config struct {
LogRollingConfig *lumberjack.Logger
LogDir string
CustomLogger Logger
LogStdout bool
}

type SamplingConfig struct {
Expand Down Expand Up @@ -108,7 +109,7 @@ func initNacosLogger(config Config) (Logger, error) {
encoder := getEncoder()
writer := config.getLogWriter()

core := zapcore.NewCore(zapcore.NewConsoleEncoder(encoder), zapcore.NewMultiWriteSyncer(writer, zapcore.AddSync(os.Stdout)), logLevel)
core := zapcore.NewCore(zapcore.NewConsoleEncoder(encoder), writer, logLevel)

if config.Sampling != nil {
core = zapcore.NewSamplerWithOptions(core, config.Sampling.Tick, config.Sampling.Initial, config.Sampling.Thereafter)
Expand Down Expand Up @@ -159,5 +160,8 @@ func (c *Config) getLogWriter() zapcore.WriteSyncer {
c.LogRollingConfig = &lumberjack.Logger{}
}
c.LogRollingConfig.Filename = c.LogDir + string(os.PathSeparator) + c.LogFileName
if c.LogStdout {
return zapcore.NewMultiWriteSyncer(zapcore.AddSync(c.LogRollingConfig), zapcore.AddSync(os.Stdout))
}
return zapcore.AddSync(c.LogRollingConfig)
}
2 changes: 2 additions & 0 deletions example/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func main() {
LogDir: "/tmp/nacos/log",
CacheDir: "/tmp/nacos/cache",
LogLevel: "debug",
LogStdout: true,
}
//or a more graceful way to create ClientConfig
_ = *constant.NewClientConfig(
Expand All @@ -57,6 +58,7 @@ func main() {
constant.WithLogDir("/tmp/nacos/log"),
constant.WithCacheDir("/tmp/nacos/cache"),
constant.WithLogLevel("debug"),
constant.WithLogStdout(true),
)

// a more graceful way to create config client
Expand Down
2 changes: 2 additions & 0 deletions example/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func main() {
CacheDir: "/tmp/nacos/cache",
LogRollingConfig: &lumberjack.Logger{MaxSize: 10},
LogLevel: "debug",
LogStdout: true,
}
//or a more graceful way to create ClientConfig
_ = *constant.NewClientConfig(
Expand All @@ -58,6 +59,7 @@ func main() {
constant.WithCacheDir("/tmp/nacos/cache"),
constant.WithLogLevel("debug"),
constant.WithLogRollingConfig(&lumberjack.Logger{MaxSize: 10}),
constant.WithLogStdout(true),
)

// a more graceful way to create naming client
Expand Down

0 comments on commit c7dfb75

Please # to comment.