Skip to content

Conversation

@ifooth
Copy link

@ifooth ifooth commented Oct 11, 2025

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

close #7609

User Case Description

@propel-code-bot
Copy link
Contributor

propel-code-bot bot commented Oct 11, 2025

Correct stack-frame resolution for slog logger and add regression test

Fixes incorrect file/line attribution in logger/slog output. Introduces a generic stack-frame helper in utils, refactors logger methods to route through a new internal log() wrapper that uses the helper, and adds a Go 1.21-only unit test that asserts the reported caller file is the test itself rather than GORM internals.

Key Changes

• Added utils.CallerFrame() to return the first non-GORM frame; rewrote utils.FileWithLineNum() on top of it
• Refactored logger/slog.go to funnel Info, Warn, Error, and Trace through new log() which creates slog.Record with utils.CallerFrame().PC
• Replaced direct Logger.InfoContext/WarnContext/ErrorContext calls with l.log() to ensure consistent caller metadata
• Added logger/slog_test.go (go1.21 build tag) validating that log output references slog_test.go and not slog.go
• Minor doc-comments and small signature tweaks (e.g., frame.PC != 0 validity check)

Affected Areas

logger/slog.go logging path
utils/utils.go stack-trace utilities
• Unit tests for logger

This summary was automatically generated by @propel-code-bot

@ifooth
Copy link
Author

ifooth commented Oct 14, 2025

Hi, @jinzhu @rezamokaram. could you please review this PR? thanks!

@jinzhu jinzhu requested a review from Copilot October 26, 2025 12:33
Copy link

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 fixes an issue where the slog logger was reporting incorrect source file locations in log output. Instead of showing the actual caller's location, it was incorrectly displaying GORM's internal slog.go file.

Key Changes:

  • Refactored FileWithLineNum() to extract frame detection logic into a reusable CallerFrame() function
  • Modified slog logger to use CallerFrame() when creating log records, ensuring accurate source location reporting
  • Added test coverage to verify that slog output correctly shows test file locations instead of internal GORM files

Reviewed Changes

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

File Description
utils/utils.go Extracted CallerFrame() function to return the actual caller's runtime frame, enabling reuse across logging implementations
logger/slog.go Implemented custom log() method that uses CallerFrame().PC to set accurate source locations in slog records
logger/slog_test.go Added test to verify slog output contains correct caller file reference and excludes internal GORM files

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

@propel-code-bot propel-code-bot bot changed the title Fix logger/slog output source file depth is incorrect Fix slog logger caller frame detection to output correct source file Oct 27, 2025
@ifooth
Copy link
Author

ifooth commented Oct 27, 2025

补充下,slog是分前端(logger), 后端Handler, 官方设计logger是可以按需修改, Handler复用,这里通过NewRecord通过Handler处理也是比较符合社区设计的

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.

logger/slog output source file depth is incorrect

1 participant