You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "geniusjoe (via GitHub)" <gi...@apache.org> on 2025/12/11 12:32:11 UTC

[PR] docs: Update example, using slog as the default logger [pulsar-client-go]

geniusjoe opened a new pull request, #1449:
URL: https://github.com/apache/pulsar-client-go/pull/1449

   Master Issue: https://github.com/apache/pulsar-client-go/pull/1234
   
   ### Motivation
   The `log/slog` package, introduced in Go 1.21 has become the standard library for structured logging, which provides support for redirecting logs to files and enabling log rotation. 
   Additionally, since pulsar default logger uses `log.NewLoggerWithLogrus(logrus.StandardLogger())`, logs cannot be output to the files when users haven't set up logrus explicit configuration. Therefore, maybe we should use `slog` and give a complete log  settings in our examples to enhance the out-of-the-box experience for users.
   
   ### Modifications
   Update log initialization configurations in example folders.
   
   ### Verifying this change
   
   - [x] Make sure that the change passes the CI checks.
   This change is a trivial rework without any test coverage.
   
   ### Does this pull request potentially affect one of the following parts:
   
   *If `yes` was chosen, please highlight the changes*
   
     - Dependencies (does it add or upgrade a dependency): (no)
     - The public API: (no)
     - The schema: (no)
     - The default values of configurations: (no)
     - The wire protocol: (no)
   
   ### Documentation
   
     - Does this pull request introduce a new feature? (no)
   
   


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] docs: Update example code, add log initialization configurations [pulsar-client-go]

Posted by "geniusjoe (via GitHub)" <gi...@apache.org>.
geniusjoe commented on code in PR #1449:
URL: https://github.com/apache/pulsar-client-go/pull/1449#discussion_r2619825902


##########
examples/consumer/consumer.go:
##########
@@ -19,18 +19,38 @@ package main
 
 import (
 	"context"
-	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
-	client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
-	if err != nil {
-		log.Fatal(err)
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
 	}
+	//	this multiLogger both print log to stdout and fileLogger
+	//  if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()

Review Comment:
   fixed



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] docs: Update example code, add log initialization configurations [pulsar-client-go]

Posted by "geniusjoe (via GitHub)" <gi...@apache.org>.
geniusjoe commented on code in PR #1449:
URL: https://github.com/apache/pulsar-client-go/pull/1449#discussion_r2619827521


##########
examples/consumer-listener/consumer-listener.go:
##########
@@ -18,45 +18,65 @@
 package main
 
 import (
-	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
-	client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
-	if err != nil {
-		log.Fatal(err)
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
 	}
+	//	this multiLogger both print log to stdout and fileLogger

Review Comment:
   fixed



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] docs: Update example code, add log initialization configurations [pulsar-client-go]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #1449:
URL: https://github.com/apache/pulsar-client-go/pull/1449#discussion_r2617748889


##########
examples/producer/producer.go:
##########
@@ -20,40 +20,58 @@ package main
 import (
 	"context"
 	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
+	}
+	//	this multiLogger both print log to stdout and fileLogger

Review Comment:
   Grammar issues in comment: "print" should be "prints" (subject-verb agreement), and "log" should be "logs" (plural).
   ```suggestion
   	//	this multiLogger prints logs to both stdout and the file via fileLogger
   ```



##########
examples/producer/producer.go:
##########
@@ -20,40 +20,58 @@ package main
 import (
 	"context"
 	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
+	}
+	//	this multiLogger both print log to stdout and fileLogger
+	//  if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()

Review Comment:
   Grammar issues in comment: missing subject "you" after "if", and "log" should be "logs" (plural).
   ```suggestion
   	//	this multiLogger prints logs to both stdout and fileLogger
   	//  if you only want to print logs to file, just pass fileLogger to slog.NewJSONHandler()
   ```



##########
examples/reader/reader.go:
##########
@@ -19,36 +19,61 @@ package main
 
 import (
 	"context"
-	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
-	client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
-	if err != nil {
-		log.Fatal(err)
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
 	}
+	//	this multiLogger both print log to stdout and fileLogger
+	//  if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()

Review Comment:
   Grammar issues in comment: missing subject "you" after "if", and "log" should be "logs" (plural).
   ```suggestion
   	//	this multiLogger prints logs to both stdout and fileLogger
   	//  if you only want to print logs to a file, just pass fileLogger to slog.NewJSONHandler()
   ```



##########
examples/consumer/consumer.go:
##########
@@ -19,18 +19,38 @@ package main
 
 import (
 	"context"
-	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
-	client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
-	if err != nil {
-		log.Fatal(err)
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
 	}
+	//	this multiLogger both print log to stdout and fileLogger

Review Comment:
   Grammar issues in comment: "print" should be "prints" (subject-verb agreement), and "log" should be "logs" (plural).
   ```suggestion
   	//	this multiLogger both prints logs to stdout and fileLogger
   ```



##########
examples/consumer-listener/consumer-listener.go:
##########
@@ -18,45 +18,65 @@
 package main
 
 import (
-	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
-	client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
-	if err != nil {
-		log.Fatal(err)
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
 	}
+	//	this multiLogger both print log to stdout and fileLogger

Review Comment:
   Grammar issues in comment: "print" should be "prints" (subject-verb agreement), and "log" should be "logs" (plural).
   ```suggestion
   	//	this multiLogger prints logs to both stdout and fileLogger
   ```



##########
examples/consumer/consumer.go:
##########
@@ -19,18 +19,38 @@ package main
 
 import (
 	"context"
-	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
-	client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
-	if err != nil {
-		log.Fatal(err)
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
 	}
+	//	this multiLogger both print log to stdout and fileLogger
+	//  if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()

Review Comment:
   Grammar issues in comment: missing subject "you" after "if", and "log" should be "logs" (plural).
   ```suggestion
   	//	this multiLogger prints logs to both stdout and fileLogger
   	//  if you only want to print logs to a file, just pass fileLogger to slog.NewJSONHandler()
   ```



##########
examples/reader/reader.go:
##########
@@ -19,36 +19,61 @@ package main
 
 import (
 	"context"
-	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
-	client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
-	if err != nil {
-		log.Fatal(err)
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
 	}
+	//	this multiLogger both print log to stdout and fileLogger
+	//  if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()
+	multiLogger := io.MultiWriter(os.Stdout, fileLogger)
+	logger := slog.New(slog.NewJSONHandler(multiLogger, &slog.HandlerOptions{Level: slog.LevelInfo}))
+	slog.SetDefault(logger)

Review Comment:
   The logger initialization code (lines 32-42) is duplicated across all example files (producer, consumer, reader, consumer-listener). Consider extracting this into a shared helper function or a common example utilities package to improve maintainability and consistency across examples.



##########
examples/reader/reader.go:
##########
@@ -19,36 +19,61 @@ package main
 
 import (
 	"context"
-	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
-	client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
-	if err != nil {
-		log.Fatal(err)
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
 	}
+	//	this multiLogger both print log to stdout and fileLogger

Review Comment:
   Grammar issues in comment: "print" should be "prints" (subject-verb agreement), and "log" should be "logs" (plural).
   ```suggestion
   	//	this multiLogger both prints logs to stdout and fileLogger
   ```



##########
examples/consumer-listener/consumer-listener.go:
##########
@@ -18,45 +18,65 @@
 package main
 
 import (
-	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
-	client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
-	if err != nil {
-		log.Fatal(err)
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
 	}
+	//	this multiLogger both print log to stdout and fileLogger
+	//  if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()

Review Comment:
   Grammar issues in comment: missing subject "you" after "if", and "log" should be "logs" (plural).
   ```suggestion
   	//	this multiLogger prints logs to both stdout and fileLogger
   	//  if you only want to print logs to a file, just pass fileLogger to slog.NewJSONHandler()
   ```



##########
examples/producer/producer.go:
##########
@@ -20,40 +20,58 @@ package main
 import (
 	"context"
 	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
+	}
+	//	this multiLogger both print log to stdout and fileLogger
+	//  if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()

Review Comment:
   Comment formatting is inconsistent: there's an extra space after the "//" on line 39, and the comment on line 41 has two spaces after "//". Comments should consistently use "// " (one space after "//").
   ```suggestion
   	// this multiLogger both print log to stdout and fileLogger
   	// if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()
   ```



##########
examples/consumer-listener/consumer-listener.go:
##########
@@ -18,45 +18,65 @@
 package main
 
 import (
-	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
-	client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
-	if err != nil {
-		log.Fatal(err)
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
 	}
+	//	this multiLogger both print log to stdout and fileLogger
+	//  if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()

Review Comment:
   Comment formatting is inconsistent: there's an extra space after the "//" on line 37, and the comment on line 39 has two spaces after "//". Comments should consistently use "// " (one space after "//").
   ```suggestion
   	// this multiLogger both print log to stdout and fileLogger
   	// if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()
   ```



##########
examples/reader/reader.go:
##########
@@ -19,36 +19,61 @@ package main
 
 import (
 	"context"
-	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
-	client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
-	if err != nil {
-		log.Fatal(err)
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
 	}
+	//	this multiLogger both print log to stdout and fileLogger
+	//  if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()

Review Comment:
   Comment formatting is inconsistent: there's an extra space after the "//" on line 38, and the comment on line 40 has two spaces after "//". Comments should consistently use "// " (one space after "//").
   ```suggestion
   	// this multiLogger both print log to stdout and fileLogger
   	// if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()
   ```



##########
examples/consumer/consumer.go:
##########
@@ -19,18 +19,38 @@ package main
 
 import (
 	"context"
-	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
-	client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
-	if err != nil {
-		log.Fatal(err)
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
 	}
+	//	this multiLogger both print log to stdout and fileLogger
+	//  if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()

Review Comment:
   Comment formatting is inconsistent: there's an extra space after the "//" on line 38, and the comment on line 40 has two spaces after "//". Comments should consistently use "// " (one space after "//").
   ```suggestion
   	// this multiLogger both print log to stdout and fileLogger
   	// if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()
   ```



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] docs: Update example code, add log initialization configurations [pulsar-client-go]

Posted by "crossoverJie (via GitHub)" <gi...@apache.org>.
crossoverJie merged PR #1449:
URL: https://github.com/apache/pulsar-client-go/pull/1449


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] docs: Update example code, add log initialization configurations [pulsar-client-go]

Posted by "geniusjoe (via GitHub)" <gi...@apache.org>.
geniusjoe commented on code in PR #1449:
URL: https://github.com/apache/pulsar-client-go/pull/1449#discussion_r2619832389


##########
examples/reader/reader.go:
##########
@@ -19,36 +19,61 @@ package main
 
 import (
 	"context"
-	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
-	client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
-	if err != nil {
-		log.Fatal(err)
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
 	}
+	//	this multiLogger both print log to stdout and fileLogger
+	//  if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()
+	multiLogger := io.MultiWriter(os.Stdout, fileLogger)
+	logger := slog.New(slog.NewJSONHandler(multiLogger, &slog.HandlerOptions{Level: slog.LevelInfo}))
+	slog.SetDefault(logger)

Review Comment:
   This is example code, and I think creating a new function to reuse the logs in each example isn't very meaningful. It's better to maintain the current state.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] docs: Update example code, add log initialization configurations [pulsar-client-go]

Posted by "geniusjoe (via GitHub)" <gi...@apache.org>.
geniusjoe commented on code in PR #1449:
URL: https://github.com/apache/pulsar-client-go/pull/1449#discussion_r2619824973


##########
examples/producer/producer.go:
##########
@@ -20,40 +20,58 @@ package main
 import (
 	"context"
 	"fmt"
-	"log"
+	"io"
+	"log/slog"
+	"os"
 
 	"github.com/apache/pulsar-client-go/pulsar"
+	pulsarlog "github.com/apache/pulsar-client-go/pulsar/log"
+	"gopkg.in/natefinch/lumberjack.v2"
 )
 
 func main() {
+	fileLogger := &lumberjack.Logger{
+		Filename:   "/tmp/pulsar-go-sdk.log",
+		MaxSize:    100,
+		MaxBackups: 5,
+		LocalTime:  true,
+	}
+	//	this multiLogger both print log to stdout and fileLogger
+	//  if only want to print log to file, just pass fileLogger to slog.NewJSONHandler()

Review Comment:
   fixed



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org