Skip to content

fix(log): guard non-Level "level" attribute against panic - #559

Open
garmr-ulfr wants to merge 3 commits into
mainfrom
assert-slog-level-attr
Open

fix(log): guard non-Level "level" attribute against panic#559
garmr-ulfr wants to merge 3 commits into
mainfrom
assert-slog-level-attr

Conversation

@garmr-ulfr

@garmr-ulfr garmr-ulfr commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

Hardens the radiance slog handler so a log call passing a non-slog.Level value keyed "level" can no longer panic the process.

Changes

  • fix(log): guard non-Level "level" attribute against panic — In log/log.go, the ReplaceAttr slog.LevelKey case asserted a.Value.Any().(slog.Level) without a comma-ok check. Replaced with a checked assertion; on a type mismatch the handler now emits a group (level=WARN, an error attr describing the unexpected type, and the original value preserved under a typed level-<T> key) instead of crashing.

Bug Fixes

  • Any log call passing an attribute keyed "level" with a non-slog.Level value crashed the whole process (previously hit by memmon's memory-tick log passing "level" as a string, worked around in 302a3ca by renaming the key). The handler is now panic-safe for that key regardless of the caller's value type.

Summary by CodeRabbit

  • Bug Fixes
    • Prevented application crashes when log entries contain an unexpected level value.
    • Added clearer diagnostic information for invalid log-level attributes while preserving the original value.

The slog handler asserted a.Value.Any().(slog.Level) unchecked in the
LevelKey case, panicking the process on any log call passing a non-Level
value keyed "level". Guard with a comma-ok assertion; on mismatch emit a
group (level=WARN, an error attr, and the original value under a typed
key) instead of crashing.
@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 3453514b-8e28-4e52-8798-2effedb456cc

📥 Commits

Reviewing files that changed from the base of the PR and between 2de1c18 and 0b7e9c4.

📒 Files selected for processing (1)
  • log/log.go

📝 Walkthrough

Walkthrough

The slog attribute replacement logic now safely handles unexpected level value types, preserving valid level formatting and returning diagnostic attributes instead of panicking.

Changes

Log level formatting fix

Layer / File(s) Summary
Guard level type assertion in ReplaceAttr
log/log.go
ReplaceAttr formats values of type slog.Level with FormatLogLevel; unexpected types produce grouped attributes containing the original value type and an error string.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: guarding the log level attribute against panics from non-slog.Level values.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch assert-slog-level-attr

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Hardens the log package’s slog handler by making ReplaceAttr resilient to callers that log a "level" attribute whose value is not a slog.Level, preventing a process panic and preserving additional context in the emitted log entry.

Changes:

  • Adds a comma-ok type assertion for "level" attributes before calling FormatLogLevel.
  • On type mismatch, emits a grouped set of attributes describing the mismatch instead of panicking.

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

Comment thread log/log.go Outdated
@garmr-ulfr
garmr-ulfr marked this pull request as ready for review July 9, 2026 00:36
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