Skip to content

docs: Update example code, add log initialization configurations#1449

Merged
crossoverJie merged 1 commit intoapache:masterfrom
geniusjoe:dev/example-with-logger
Dec 22, 2025
Merged

docs: Update example code, add log initialization configurations#1449
crossoverJie merged 1 commit intoapache:masterfrom
geniusjoe:dev/example-with-logger

Conversation

@geniusjoe
Copy link
Copy Markdown
Contributor

@geniusjoe geniusjoe commented Dec 11, 2025

Master Issue: #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

Add log initialization configurations in example files.

Verifying this change

  • 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)

@geniusjoe geniusjoe changed the title docs: Update example, using slog as the default logger docs: Update example code, using slog as the default logger Dec 11, 2025
@geniusjoe geniusjoe changed the title docs: Update example code, using slog as the default logger docs: Update example code, add log initialization configurations Dec 11, 2025
@geniusjoe geniusjoe force-pushed the dev/example-with-logger branch 2 times, most recently from 09b8f0e to db93d24 Compare December 12, 2025 17:29
@crossoverJie crossoverJie requested a review from Copilot December 15, 2025 02:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates example files to use Go's standard log/slog package (introduced in Go 1.21) for structured logging, replacing the previous basic logging approach. It adds comprehensive logging configuration with file output and rotation capabilities via the lumberjack library to improve the out-of-the-box experience for users.

Key Changes:

  • Adds gopkg.in/natefinch/lumberjack.v2 dependency for log rotation support
  • Updates all four example files (producer, consumer, reader, consumer-listener) with complete slog logger initialization
  • Replaces log.Fatal() and fmt.Printf() calls with structured logger.Error() and logger.Info() calls

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 13 comments.

Show a summary per file
File Description
go.mod Adds lumberjack.v2 dependency for log rotation functionality
go.sum Adds checksums for lumberjack.v2 dependency
examples/reader/reader.go Implements slog logger with file rotation and updates all logging calls to use structured logging
examples/producer/producer.go Implements slog logger with file rotation and updates all logging calls to use structured logging
examples/consumer/consumer.go Implements slog logger with file rotation and updates all logging calls to use structured logging
examples/consumer-listener/consumer-listener.go Implements slog logger with file rotation and updates all logging calls to use structured logging

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread examples/producer/producer.go Outdated
Comment thread examples/consumer-listener/consumer-listener.go Outdated
Comment thread examples/reader/reader.go Outdated
Comment thread examples/consumer/consumer.go Outdated
Comment thread examples/producer/producer.go Outdated
Comment thread examples/reader/reader.go Outdated
Comment thread examples/producer/producer.go Outdated
Comment thread examples/consumer/consumer.go Outdated
Comment thread examples/consumer-listener/consumer-listener.go Outdated
Comment thread examples/reader/reader.go
@geniusjoe geniusjoe force-pushed the dev/example-with-logger branch from 9e0e22f to 95679a7 Compare December 15, 2025 15:07
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@geniusjoe geniusjoe force-pushed the dev/example-with-logger branch from 95679a7 to 8af6732 Compare December 15, 2025 15:09
@crossoverJie crossoverJie added this to the v0.19.0 milestone Dec 16, 2025
@crossoverJie crossoverJie merged commit 3bb7d4e into apache:master Dec 22, 2025
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants