Skip to content

fix: preserve custom logger option and add WithStructuredLogging - #2712

Merged
kgala2 merged 1 commit into
GoogleCloudPlatform:mainfrom
reginaldalfret:fix/preserve-custom-logger-option
Nov 4, 2026
Merged

kgala2 merged 1 commit into
GoogleCloudPlatform:mainfrom
reginaldalfret:fix/preserve-custom-logger-option

Conversation

@reginaldalfret

Copy link
Copy Markdown
Contributor

Description of changes

This change ensures that a custom logger supplied via the programmatic WithLogger option is preserved when CLI flags or configuration files specify --structured-logs or --quiet.

  1. Preserve custom logger:

    • Previously in loadConfig, c.logger was unconditionally re-assigned if c.conf.StructuredLogs or c.conf.Quiet was true, overwriting any custom cloudsql.Logger passed by the caller in WithLogger.
    • Added a customLogger indicator to Command set by WithLogger so that custom loggers take precedence over default logger instantiation.
  2. Add WithStructuredLogging() Option:

    • Added WithStructuredLogging() to cmd/options.go alongside WithQuietLogging() and WithDebugLogging(), allowing programmatic enablement of structured JSON logging.
  3. Regression Tests:

    • Added WithStructuredLogging test case to TestCommandOptions.
    • Added TestCommandOptionsOverridesCLI test cases verifying that WithLogger takes precedence when --structured-logs or --quiet flags are supplied.

Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>
@reginaldalfret
reginaldalfret requested a review from a team as a code owner October 26, 2026 10:46

@blacksoxx blacksoxx left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ran this locally at d8a38c5: it merges clean with current main, go test -race ./cmd -run TestCommandOptions passes, and with only the two new WithLogger cases applied on main both fail with "custom logger was overridden", so the tests pin the behaviour change. go vet is clean.

The customLogger flag reads fine to me and keeps --quiet and --structured-logs working for callers who do not pass a logger. Nothing else from my side; the failing E2E check is the usual secrets gate for outside PRs rather than anything in this diff.

@kgala2

kgala2 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

/gcbrun

@kgala2

kgala2 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Thanks for the PR @reginaldalfret! we would have the custom logger option fix rolled out in the next release.

@kgala2
kgala2 merged commit 4ec343a into GoogleCloudPlatform:main Nov 4, 2026
19 of 22 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.

4 participants