chengxilo commented on code in PR #3379:
URL: https://github.com/apache/iggy/pull/3379#discussion_r3360110266
##########
foreign/go/client/iggy_client.go:
##########
@@ -54,8 +56,19 @@ func WithTcp(tcpOpts ...tcp.Option) Option {
}
}
+// WithLogger sets the logger for the Iggy client and its underlying transport.
+// This logger is used by the heartbeat and forwarded to the transport as a
+// default. When no logger is provided, all internal log output is silently
+// discarded.
+func WithLogger(logger *slog.Logger) Option {
+ return func(opts *Options) {
+ opts.logger = logger
+ }
+}
+
Review Comment:
```suggestion
func WithLogger(logger *slog.Logger) Option {
return func(opts *Options) {
if logger == nil {
return
}
opts.logger = logger
}
}
```
`WithLogger(nil)` will stores a nil `*slog.Logger`, overwriting the
`DiscardHandler` default from `GetDefaultOptions`. This nil propagates to both
`IggyClient.logger` and `IggyTcpClient.logger`.
##########
foreign/go/client/iggy_client_test.go:
##########
@@ -0,0 +1,59 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package client
+
+import (
+ "bytes"
+ "log/slog"
+ "strings"
+ "testing"
+)
+
+func TestGetDefaultOptions_ProvidesLogger(t *testing.T) {
+ opts := GetDefaultOptions()
+ if opts.logger == nil {
+ t.Fatal("expected default options to include a non-nil logger")
+ }
+}
Review Comment:
```suggestion
```
unnecessary
##########
foreign/go/client/iggy_client_test.go:
##########
@@ -0,0 +1,59 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package client
+
+import (
+ "bytes"
+ "log/slog"
+ "strings"
+ "testing"
+)
+
+func TestGetDefaultOptions_ProvidesLogger(t *testing.T) {
+ opts := GetDefaultOptions()
+ if opts.logger == nil {
+ t.Fatal("expected default options to include a non-nil logger")
+ }
+}
+
Review Comment:
As we will provide a nil check, I recommend test it to prevent regression.
```suggestion
func TestWithLogger_Nil(t *testing.T) {
cli, err := NewIggyClient(WithLogger(nil))
if err != nil {
t.Fatalf("unexpected error creating client: %v", err)
}
ic := cli.(*IggyClient)
if ic.logger.Handler() != slog.DiscardHandler {
t.Errorf("expected slog.DiscardHandler, get %v",
ic.logger.Handler())
}
}
```
##########
foreign/go/client/iggy_client_test.go:
##########
@@ -0,0 +1,59 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package client
+
+import (
+ "bytes"
+ "log/slog"
+ "strings"
+ "testing"
+)
+
+func TestGetDefaultOptions_ProvidesLogger(t *testing.T) {
+ opts := GetDefaultOptions()
+ if opts.logger == nil {
+ t.Fatal("expected default options to include a non-nil logger")
+ }
+}
+
+func TestWithLogger_SetsClientLogger(t *testing.T) {
+ var buf bytes.Buffer
+ logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{
+ Level: slog.LevelDebug,
+ }))
+
+ cli, err := NewIggyClient(WithLogger(logger))
+ if err != nil {
+ t.Fatalf("unexpected error creating client: %v", err)
+ }
+
+ ic, ok := cli.(*IggyClient)
+ if !ok {
+ t.Fatal("expected *IggyClient from NewIggyClient")
+ }
Review Comment:
```suggestion
ic := cli.(*IggyClient)
```
it will always be a IggyClient so the check is not needed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]