Merged
Conversation
- 统一错误字段名为 "error",使用常量定义字段键
- 修复 Error 系列函数的 nil 处理,返回 slog.Attr{} 而非空字段
- 添加 Uint64 和 Group 字段构造函数
- 改进 getStackTrace 实现,过滤 runtime.main 和 runtime.goexit
- 清理冗余注释,保持代码简洁
- 更新 README.md 文档,反映最新 API 变更
Breaking changes:
- Error 字段名从 "err_msg" 改为 "msg"(在 error group 中)
- nil 错误不再记录任何字段(之前会记录空字段或 code)
- 移除 WithStandardContext,改用更灵活的 WithContextField - 修复 Level 常量定义,使用显式值替代 iota - 修复 With 方法字段污染 bug,避免派生 Logger 之间共享底层数组 - 统一错误字段命名,使用 err_msg 避免与标准 msg 冲突 - 重构 Handler 文件结构,slog_handler.go 重命名为 handler.go - 完善示例代码,添加预设字段和动态级别演示 - 添加回归测试 TestLoggerWith_DerivedLoggerDoesNotMutateSiblings Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概述
本次 PR 对
clog组件进行了重要改进,包括 API 重构、Bug 修复和代码质量提升,为 v0.4.0 版本发布做准备。主要变更
1. API 改进
WithStandardContext():该函数强制使用固定的字段名(trace_id, user_id, request_id),缺乏灵活性WithContextField():允许用户自定义从 Context 中提取任意字段,提供更好的可控性2. 修复 With 字段污染 Bug
问题描述:使用
With()创建派生 Logger 时,如果基础 Logger 的baseAttrs数组有多余容量,后续派生操作可能会覆盖已有字段。修复方案:在
With()方法中显式复制baseAttrs数组,避免多个派生 Logger 共享底层数组。回归测试:新增
TestLoggerWith_DerivedLoggerDoesNotMutateSiblings测试用例,覆盖该边界场景。3. 修复 Level 常量定义
改用显式值定义,确保与 slog.Level 完全对齐:
4. 统一错误字段设计
使用
err_msg作为错误消息字段名,避免与 slog 标准msg字段冲突。5. 代码质量提升
slog_handler.go→handler.go测试计划
Breaking Changes
WithStandardContext()已移除,需改用WithContextField()组合🤖 Generated with Claude Code