Skip to content

Commit c7dfb75

Browse files
chenzfpchenzf72
and
chenzf72
authored
bugfix dubbo-go issue: nacos-go-sdk stdout redirect #1960 (#489)
fix:490 Co-authored-by: chenzf72 <[email protected]>
1 parent 24e67a3 commit c7dfb75

File tree

7 files changed

+19
-1
lines changed

7 files changed

+19
-1
lines changed

Diff for: clients/config_client/config_client.go

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func NewConfigClient(nc nacos_client.INacosClient) (*ConfigClient, error) {
101101
LogRollingConfig: clientConfig.LogRollingConfig,
102102
LogDir: clientConfig.LogDir,
103103
CustomLogger: clientConfig.CustomLogger,
104+
LogStdout: clientConfig.LogStdout,
104105
}
105106
err = logger.InitLogger(loggerConfig)
106107
if err != nil {

Diff for: clients/naming_client/naming_client.go

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func NewNamingClient(nc nacos_client.INacosClient) (NamingClient, error) {
7474
LogRollingConfig: clientConfig.LogRollingConfig,
7575
LogDir: clientConfig.LogDir,
7676
CustomLogger: clientConfig.CustomLogger,
77+
LogStdout: clientConfig.LogStdout,
7778
}
7879
err = logger.InitLogger(loggerConfig)
7980
if err != nil {

Diff for: common/constant/client_config_options.go

+7
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,10 @@ func WithLogRollingConfig(rollingConfig *lumberjack.Logger) ClientOption {
180180
config.LogRollingConfig = rollingConfig
181181
}
182182
}
183+
184+
// WithLogStdout ...
185+
func WithLogStdout(logStdout bool) ClientOption {
186+
return func(config *ClientConfig) {
187+
config.LogStdout = logStdout
188+
}
189+
}

Diff for: common/constant/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ type ClientConfig struct {
5252
ContextPath string // the nacos server contextpath
5353
LogRollingConfig *lumberjack.Logger // the log rolling config
5454
CustomLogger logger.Logger // the custom log interface ,With a custom Logger (nacos sdk will not provide log cutting and archiving capabilities)
55+
LogStdout bool // the stdout redirect for log, default is false
5556
}

Diff for: common/logger/logger.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Config struct {
4545
LogRollingConfig *lumberjack.Logger
4646
LogDir string
4747
CustomLogger Logger
48+
LogStdout bool
4849
}
4950

5051
type SamplingConfig struct {
@@ -108,7 +109,7 @@ func initNacosLogger(config Config) (Logger, error) {
108109
encoder := getEncoder()
109110
writer := config.getLogWriter()
110111

111-
core := zapcore.NewCore(zapcore.NewConsoleEncoder(encoder), zapcore.NewMultiWriteSyncer(writer, zapcore.AddSync(os.Stdout)), logLevel)
112+
core := zapcore.NewCore(zapcore.NewConsoleEncoder(encoder), writer, logLevel)
112113

113114
if config.Sampling != nil {
114115
core = zapcore.NewSamplerWithOptions(core, config.Sampling.Tick, config.Sampling.Initial, config.Sampling.Thereafter)
@@ -159,5 +160,8 @@ func (c *Config) getLogWriter() zapcore.WriteSyncer {
159160
c.LogRollingConfig = &lumberjack.Logger{}
160161
}
161162
c.LogRollingConfig.Filename = c.LogDir + string(os.PathSeparator) + c.LogFileName
163+
if c.LogStdout {
164+
return zapcore.NewMultiWriteSyncer(zapcore.AddSync(c.LogRollingConfig), zapcore.AddSync(os.Stdout))
165+
}
162166
return zapcore.AddSync(c.LogRollingConfig)
163167
}

Diff for: example/config/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func main() {
4848
LogDir: "/tmp/nacos/log",
4949
CacheDir: "/tmp/nacos/cache",
5050
LogLevel: "debug",
51+
LogStdout: true,
5152
}
5253
//or a more graceful way to create ClientConfig
5354
_ = *constant.NewClientConfig(
@@ -57,6 +58,7 @@ func main() {
5758
constant.WithLogDir("/tmp/nacos/log"),
5859
constant.WithCacheDir("/tmp/nacos/cache"),
5960
constant.WithLogLevel("debug"),
61+
constant.WithLogStdout(true),
6062
)
6163

6264
// a more graceful way to create config client

Diff for: example/service/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func main() {
4848
CacheDir: "/tmp/nacos/cache",
4949
LogRollingConfig: &lumberjack.Logger{MaxSize: 10},
5050
LogLevel: "debug",
51+
LogStdout: true,
5152
}
5253
//or a more graceful way to create ClientConfig
5354
_ = *constant.NewClientConfig(
@@ -58,6 +59,7 @@ func main() {
5859
constant.WithCacheDir("/tmp/nacos/cache"),
5960
constant.WithLogLevel("debug"),
6061
constant.WithLogRollingConfig(&lumberjack.Logger{MaxSize: 10}),
62+
constant.WithLogStdout(true),
6163
)
6264

6365
// a more graceful way to create naming client

0 commit comments

Comments
 (0)