Thanks to visit codestin.com
Credit goes to github.com

Skip to content

🐛 bug: prevent panics on non-string log keys#4046

Merged
ReneWerner87 merged 2 commits intomainfrom
update-privatelogw-for-safe-non-string-keys
Feb 3, 2026
Merged

🐛 bug: prevent panics on non-string log keys#4046
ReneWerner87 merged 2 commits intomainfrom
update-privatelogw-for-safe-non-string-keys

Conversation

@gaby
Copy link
Member

@gaby gaby commented Feb 3, 2026

Motivation

  • Prevent panics when non-string types are passed as keys to structured logging helpers (e.g. Tracew, Infow) by making key handling safe and stable.

Description

  • Replace forced .(string) assertion in log/default.go with a type switch that writes string keys directly and formats non-string keys via fmt.Fprint, and add Test_DefaultLoggerNonStringKeys to log/default_test.go to exercise Tracew/Infow with non-string keys and assert no panics and stable output.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @gaby, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical bug in the logging system where passing non-string values as keys to structured logging functions could lead to application panics. By introducing a type-safe approach to key handling, the change ensures that all key types are gracefully converted to their string representation, significantly enhancing the stability and reliability of the logging infrastructure.

Highlights

  • Panic Prevention: Implemented a robust mechanism to prevent panics in structured logging functions (e.g., Tracew, Infow) when non-string types are used as keys.
  • Type-Safe Key Handling: Replaced a direct .(string) type assertion with a type switch to safely handle and format non-string log keys using fmt.Fprint.
  • New Test Coverage: Added Test_DefaultLoggerNonStringKeys to log/default_test.go to ensure the new key handling logic works as expected and prevents panics with various non-string key types.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • log/default.go
    • Replaced a direct string type assertion for log keys with a type switch to handle string keys directly and format other types using fmt.Fprint.
  • log/default_test.go
    • Introduced a new test case, Test_DefaultLoggerNonStringKeys, to validate the correct behavior and panic prevention of structured logging functions when non-string keys are provided.
    • Added a custom stringKey struct to demonstrate handling of user-defined types in log keys.
Activity
  • The make audit command reported 11 standard library vulnerabilities due to govulncheck with go1.25.1.
  • All other make commands (generate, betteralign, modernize, format, lint) completed successfully.
  • All 2609 tests passed with 2 skipped during make test.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 3, 2026

Walkthrough

Structured logging was changed to handle non-string keys in log output. privateLogw now uses a type switch to write string keys directly and format non-string keys with fmt.Fprint. Tests were added to validate non-string key formatting for Tracew and Infow.

Changes

Cohort / File(s) Summary
Non-string Key Handling
log/default.go
Replaced a direct .(string) assertion with a type switch in privateLogw to support non-string keys without panics, preserving string-key formatting.
Test Coverage
log/default_test.go
Added Test_DefaultLoggerNonStringKeys with subtests for Tracew and Infow to assert logs include formatted non-string keys. Tests use in-memory buffer and run in parallel.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

☢️ Bug, v3

Suggested reviewers

  • sixcolors
  • ReneWerner87
  • efectn

Poem

🐰 I hopped in code where keys once tripped,
A type-switch bridge where panics slipped.
Strings skip through, others get shown,
Logs now sing in every tone. 🥕✨

🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description check ⚠️ Warning The description provides clear motivation and describes the specific changes made, but it lacks completion of the required template sections including Type of change, Checklist items, and structured changelog information. Complete the required template by selecting Type of change (Code consistency and/or Bug fix), checking relevant checklist items (unit tests added/passing), and adding a Changelog/What's New entry summarizing the fix.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: preventing panics when non-string keys are passed to logging helpers, which directly matches the changeset in log/default.go and tests.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch update-privatelogw-for-safe-non-string-keys

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.

@ReneWerner87 ReneWerner87 added this to v3 Feb 3, 2026
@ReneWerner87 ReneWerner87 added this to the v3 milestone Feb 3, 2026
@gaby gaby moved this to In Progress in v3 Feb 3, 2026
@codecov
Copy link

codecov bot commented Feb 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.20%. Comparing base (81435e6) to head (2312911).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4046      +/-   ##
==========================================
- Coverage   91.21%   91.20%   -0.01%     
==========================================
  Files         119      119              
  Lines       11117    11121       +4     
==========================================
+ Hits        10140    10143       +3     
- Misses        620      621       +1     
  Partials      357      357              
Flag Coverage Δ
unittests 91.20% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively resolves a panic caused by non-string keys in structured logging functions by replacing a risky type assertion with a safe type switch. The added test case confirms the fix. My review includes a suggestion to refactor the new test to use sub-tests, which will improve test isolation and maintainability. Overall, this is a solid improvement to the logging utility's robustness.

Copy link
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

Prevents panics in structured logging (*w helpers) when callers pass non-string keys by safely stringifying keys before writing them to the log buffer.

Changes:

  • Replaced the forced .(string) key assertion in defaultLogger.privateLogw with safe handling via a type switch (string keys written directly; other key types formatted).
  • Added a regression test covering non-string keys for Tracew/Infow, asserting no panics and deterministic output.

Reviewed changes

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

File Description
log/default.go Makes privateLogw robust to non-string keys by avoiding a panic-causing type assertion.
log/default_test.go Adds coverage ensuring non-string keys do not panic and produce stable log output.

@ReneWerner87 ReneWerner87 merged commit 7eca620 into main Feb 3, 2026
17 checks passed
@ReneWerner87 ReneWerner87 deleted the update-privatelogw-for-safe-non-string-keys branch February 3, 2026 07:03
@github-project-automation github-project-automation bot moved this from In Progress to Done in v3 Feb 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants