chengxilo commented on code in PR #3379:
URL: https://github.com/apache/iggy/pull/3379#discussion_r3338250730
##########
foreign/go/client/tcp/tcp_core.go:
##########
@@ -203,8 +217,14 @@ func NewIggyTcpClient(options ...Option) *IggyTcpClient {
}
}
+ logger := opts.logger
+ if logger == nil {
+ logger = iggcon.NopLogger()
+ }
Review Comment:
This if statement can be removed. We can just move the default value into
the GetDefaultOptions() at L44.
##########
foreign/go/client/tcp/tcp_core.go:
##########
Review Comment:
```suggestion
config: defaultTcpClientConfig(),
logger: iggcon.NopLogger() // Or just
slog.New(slog.DiscardHandler) should be better in my opinion
```
##########
foreign/go/client/iggy_client.go:
##########
@@ -72,15 +86,26 @@ func NewIggyClient(options ...Option) (iggcon.Client,
error) {
opt(&opts)
}
+ logger := opts.logger
+ if logger == nil {
+ logger = iggcon.NopLogger()
+ }
Review Comment:
same here, move it to GetDefaultOptions(). We set NopLogger() in default
options, and let the
```go
for _, opt := range options {
opt(&opts)
}
```
to overwrite it.
##########
foreign/go/client/tcp/tcp_core.go:
##########
@@ -203,8 +217,14 @@ func NewIggyTcpClient(options ...Option) *IggyTcpClient {
}
}
+ logger := opts.logger
+ if logger == nil {
+ logger = iggcon.NopLogger()
+ }
+
return &IggyTcpClient{
config: opts.config,
+ logger: logger,
Review Comment:
```suggestion
logger: opts.logger,
```
##########
foreign/go/contracts/logger.go:
##########
@@ -0,0 +1,36 @@
+// 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 iggcon
+
+import (
+ "io"
+ "log/slog"
+)
+
+// NewLogger creates a text-format logger that writes to writer at the given
minimum level.
+func NewLogger(writer io.Writer, level slog.Level) *slog.Logger {
+ handler := slog.NewTextHandler(writer, &slog.HandlerOptions{
+ Level: level,
+ })
+ return slog.New(handler)
+}
+
+// NopLogger returns a logger that silently discards all output.
+func NopLogger() *slog.Logger {
+ return slog.New(slog.DiscardHandler)
+}
Review Comment:
Maybe we can delete `logger.go` and `logger_test.go`. If we use
`*slog.Logger`, we can just inline `slog.New(slog.DiscardHandler)` for default
logger. And for user who want to see the logs, they can do
```go
client.NewIggyClient(client.WithLogger(slog.New(slog.NewTextHandler(
os.Stdout,
&slog.HandlerOptions{
Level: slog.LevelDebug,
}))))
```
So the `NewLogger` won't be actually used.
The NopLogger makes sense, but I think it's also fine to just inline
```go
slog.New(slog.DiscardHandler)
```
So we can remove both of them, and the corresponding tests.
##########
foreign/go/internal/util/leader_aware.go:
##########
@@ -31,25 +31,39 @@ import (
// CheckAndRedirectToLeader queries the client for cluster metadata and returns
// an address to redirect to (empty string means no redirection needed).
-func CheckAndRedirectToLeader(ctx context.Context, c iggcon.Client,
currentAddress string, transport iggcon.Protocol) (string, error) {
- log.Println("Checking cluster metadata for leader detection")
+func CheckAndRedirectToLeader(ctx context.Context, c iggcon.Client,
currentAddress string, transport iggcon.Protocol, logger *slog.Logger) (string,
error) {
+ if logger == nil {
+ logger = iggcon.NopLogger()
+ }
Review Comment:
Regarding here, if you change the `GetDefaultOptions()`, it should be safe
to remove the nil check.
--
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]