Skip to content

feat: switch SetupLogger to slog-native log.SetDefault#79

Merged
ankurs merged 6 commits intomainfrom
feat/slog-native-setup
Apr 12, 2026
Merged

feat: switch SetupLogger to slog-native log.SetDefault#79
ankurs merged 6 commits intomainfrom
feat/slog-native-setup

Conversation

@ankurs
Copy link
Copy Markdown
Member

@ankurs ankurs commented Apr 12, 2026

Summary

  • Replace log.SetLogger(log.NewLogger(cbslog.NewLogger(...))) with log.SetDefault(log.NewHandler(...))
  • log.SetDefault also calls slog.SetDefault, enabling native slog.LogAttrs calls to automatically get ColdBrew context fields (trace ID, gRPC method, etc.)
  • Remove cbslog (loggers/slog) import — backend logic is now in log.Handler
  • Bump go-coldbrew/log to v0.4.0

Why

Without this change, core.New() sets up logging via the deprecated BaseLogger path, which never calls slog.SetDefault. This means native slog.LogAttrs calls in service code don't get ColdBrew context fields — breaking the cookiecutter template's switch to slog.LogAttrs (go-coldbrew/cookiecutter-coldbrew#34).

Test plan

  • Code change is minimal (5 lines changed in SetupLogger)
  • make test — CI will verify
  • make lint — CI will verify

Ref: go-coldbrew/log#27, go-coldbrew/cookiecutter-coldbrew#34

Summary by CodeRabbit

  • Chores
    • Bumped the logging dependency and updated logger behavior so configuration now preserves any existing logger and applies levels safely, while still initializing a default logger when none is present.
  • Tests
    • Improved logger-related tests to verify global logging integration and to ensure configuration is isolated and restored during test runs.

Replace BaseLogger-based setup with log.SetDefault(log.NewHandler(...))
which also calls slog.SetDefault, enabling native slog.LogAttrs calls
to automatically get ColdBrew context fields.

- Remove cbslog (loggers/slog) import — backend logic now in log.Handler
- Bump log to v0.4.0
- SetupLogger parses level first, passes it to NewHandler in one call

Ref: go-coldbrew/log#27
Copilot AI review requested due to automatic review settings April 12, 2026 04:12
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 12, 2026

Warning

Rate limit exceeded

@ankurs has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 1 minutes and 23 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 1 minutes and 23 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0b22a07b-d011-4791-95b5-82a51aaa20d8

📥 Commits

Reviewing files that changed from the base of the PR and between f74ce7a and a44afd7.

📒 Files selected for processing (1)
  • core_coverage_test.go
📝 Walkthrough

Walkthrough

Dependency github.com/go-coldbrew/log bumped to v0.4.1. SetupLogger now respects an existing ColdBrew handler (applies level only) or installs a slog-native ColdBrew log.Handler via log.SetDefault(log.NewHandler(...)); parse errors logged as structured fields. Tests snapshot/restore slog.Default() and ColdBrew handler and add a test ensuring existing handlers are preserved.

Changes

Cohort / File(s) Summary
Dependency bump
go.mod
Updated github.com/go-coldbrew/log from v0.3.2 to v0.4.1.
Logger wiring
initializers.go
SetupLogger no longer unconditionally creates a ColdBrew logger or replaces the handler; it parses level, logs parse errors as structured fields, checks log.DefaultIsSet() and either sets level only or installs a slog-native log.Handler with log.SetDefault(log.NewHandler(...)).
Tests — handler preservation & restore
core_coverage_test.go
Tests now snapshot and restore slog.Default() and ColdBrew handler via t.Cleanup; assert slog.Default().Handler() is a *log.Handler; add TestSetupLogger_RespectsExistingHandler to verify existing handler is left unchanged while level is applied.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐇 I hopped through versions, found a handler new,
I nudged the level gently, left old friends true.
Parsed a messy level, tucked the error in a field,
Set defaults where empty, kept the rest concealed.
Hoppity-hop — the logs now hum, all neat and true.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: switch SetupLogger to slog-native log.SetDefault' accurately captures the main change: migrating the logger setup from the old API to slog's native SetDefault pattern, which is the core objective of the PR.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/slog-native-setup

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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

Updates core logging initialization to use ColdBrew’s slog-native handler via log.SetDefault, ensuring native slog.LogAttrs calls receive ColdBrew context fields.

Changes:

  • Switch SetupLogger from the deprecated SetLogger(NewLogger(...)) path to SetDefault(NewHandler(...)).
  • Remove the log/loggers/slog (cbslog) import and rely on handler-based configuration.
  • Bump github.com/go-coldbrew/log dependency to v0.4.0.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
initializers.go Updates SetupLogger to use log.SetDefault(log.NewHandler(...)) and adjusts comments/imports accordingly.
go.mod Bumps github.com/go-coldbrew/log to v0.4.0.

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

Comment thread initializers.go Outdated
Comment thread initializers.go
Comment thread go.mod
… test

- Fix misordered log fields: use "msg" for message, "err" for the error
- Add assertion that SetupLogger wires slog.Default to *log.Handler
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

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.


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

Comment thread core_coverage_test.go Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@core_coverage_test.go`:
- Around line 1313-1316: The test cleanup currently calls
slog.SetDefault(prevSlog) before log.SetDefault(prevHandler), but
log.SetDefault(...) internally calls slog.SetDefault(...) and thus will
overwrite the explicit slog restoration; swap the calls so
log.SetDefault(prevHandler) runs first and then slog.SetDefault(prevSlog) to
ensure slog's state is correctly restored. Update the t.Cleanup anonymous func
that references slog.SetDefault, log.SetDefault, prevSlog and prevHandler
accordingly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6a7d1ae9-fde4-454c-8030-1aafa0ffcd54

📥 Commits

Reviewing files that changed from the base of the PR and between 6b7de29 and bdeb379.

📒 Files selected for processing (1)
  • core_coverage_test.go

Comment thread core_coverage_test.go
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

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.


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

Comment thread core_coverage_test.go Outdated
SetupLogger now checks log.DefaultIsSet() before overwriting — if the
user called log.SetDefault in init(), core only updates the log level.

- Bump log to v0.4.1 (adds DefaultIsSet)
- Fix error log field ordering in SetupLogger
- Add TestSetupLogger_RespectsExistingHandler
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
core_coverage_test.go (1)

1313-1316: ⚠️ Potential issue | 🟡 Minor

Swap cleanup order to correctly restore global slog state.

Line 1314 and Line 1335 restore slog before log, but log.SetDefault(...) can reset slog again. Restore log first, then slog, in both cleanup blocks.

🔧 Suggested fix
 	t.Cleanup(func() {
-		slog.SetDefault(prevSlog)
-		log.SetDefault(prevHandler)
+		log.SetDefault(prevHandler)
+		slog.SetDefault(prevSlog)
 	})
...
 	t.Cleanup(func() {
-		slog.SetDefault(prevSlog)
-		log.SetDefault(prevHandler)
+		log.SetDefault(prevHandler)
+		slog.SetDefault(prevSlog)
 	})
#!/bin/bash
# Verify cleanup ordering in this test file and check SetupLogger/log.SetDefault usage context.
rg -n -C3 't\.Cleanup\(func\(\)\s*\{|slog\.SetDefault\(prevSlog\)|log\.SetDefault\(prevHandler\)' core_coverage_test.go
rg -n -C4 'func SetupLogger|log\.SetDefault\(' initializers.go

Also applies to: 1334-1337

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@core_coverage_test.go` around lines 1313 - 1316, The cleanup blocks currently
restore slog before log which can cause log.SetDefault to reconfigure slog; in
each t.Cleanup where you call slog.SetDefault(prevSlog) and
log.SetDefault(prevHandler) (e.g., the blocks referencing prevSlog and
prevHandler), swap the order so you call log.SetDefault(prevHandler) first and
then slog.SetDefault(prevSlog); update both occurrences (around the current
t.Cleanup blocks) to restore log first, then slog to reliably restore global
state after SetupLogger/log.SetDefault usage.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@core_coverage_test.go`:
- Around line 1313-1316: The cleanup blocks currently restore slog before log
which can cause log.SetDefault to reconfigure slog; in each t.Cleanup where you
call slog.SetDefault(prevSlog) and log.SetDefault(prevHandler) (e.g., the blocks
referencing prevSlog and prevHandler), swap the order so you call
log.SetDefault(prevHandler) first and then slog.SetDefault(prevSlog); update
both occurrences (around the current t.Cleanup blocks) to restore log first,
then slog to reliably restore global state after SetupLogger/log.SetDefault
usage.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d4bc0aa9-91a6-4874-aa1f-73b04efd85d9

📥 Commits

Reviewing files that changed from the base of the PR and between bdeb379 and f74ce7a.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (3)
  • core_coverage_test.go
  • go.mod
  • initializers.go
✅ Files skipped from review due to trivial changes (1)
  • go.mod
🚧 Files skipped from review as they are similar to previous changes (1)
  • initializers.go

@ankurs ankurs merged commit 9c4cb95 into main Apr 12, 2026
7 checks passed
@ankurs ankurs deleted the feat/slog-native-setup branch April 12, 2026 06:52
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.

2 participants