Repository navigation
feat(os/glog): add a slog handler forwarding to glog - #4986
Conversation
Add NewSlogHandler, a slog.Handler that forwards records to a glog logger. Levels map to Debug/Info/Notice/Warning/Error, attributes are appended to the message as key=value pairs with group prefixes, and Enabled follows the logger level configuration. Existing logging behavior is untouched when the handler is not used.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #4986 +/- ##
==========================================
+ Coverage 84.98% 85.04% +0.05%
==========================================
Files 892 893 +1
Lines 47047 47151 +104
==========================================
+ Hits 39984 40098 +114
+ Misses 7063 7053 -10
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thanks for the PR. I ran it in an external module (gf resolved under a 1. Caller info is wrong in The auto stack on 2. Attributes are flattened into the message text. Values are not quoted, so a value with a newline produces a fake log line. With 3. Tests don't catch several changes. |
Forward record attributes as key-value pairs so glog handlers format them like direct logging calls, and use the slog record PC for caller and error stack information.
…t/glog-slog-handler
|
Thanks for the review. All three points are addressed in the latest push. 1. Caller info with
2. Attributes Attributes are no longer flattened into the message. They are forwarded as key-value pairs in glog's
To be explicit about the default text handler: glog natively joins values with spaces and does not quote them ( 3. Tests Added tests that fail on the mutations you listed:
Local runs: |
|
@LanceAdd thanks, I re-ran the same probes against
Two edge cases I hit that are not blockers: 1.
2. |
|
这次提交的
当前提交的各项 CI 检查均已通过,此前评审中提出的问题也已在当前代码中确认解决,整体符合合入要求。 |
背景
标准库
log/slog已成为结构化日志的统一入口,glog 还没有接入方式。新增一个把 slog 记录转发到 glog 的slog.Handler,方便已有 slog 用法的项目继续输出到 glog,且不改变现有日志路径。改动
新增
glog.NewSlogHandler(logger ...*Logger),返回*glog.SlogHandler;不传 logger 时使用默认 logger。Debug→LEVEL_DEBU;Info→LEVEL_INFO;Info与Warn之间的自定义级别 →LEVEL_NOTI;Warn..Error之前 →LEVEL_WARN;Error及以上 →LEVEL_ERRO。Values,等价于直接调用logger.Info(ctx, msg, k, v, ...);slog.Group与WithGroup展开为group.key;LogValuer先 Resolve,空 Attr 跳过。默认文本 handler 按 glog 原生方式空格拼接,HandlerStructure/HandlerJson按结构化字段输出(需要时自动加引号)。record.PC推导调用点,-trimpath构建下同样正确;Error级别的栈也从record.PC起算,并沿用 logger 的栈过滤配置。SetLevel后 slog 侧即可跳过被过滤的级别。WithAttrs、WithGroup返回新的 handler,不修改原对象,可安全并发使用。示例:
兼容性
Logger现有方法和公开 API。print内部拆出printWithCaller用于注入 caller PC,GetStack的过滤器构造提取为stackFilters(),均行为不变。测试
go test ./os/glog/ -count=1全包通过;Test_SlogHandler在-race与-trimpath下通过。Resolve、去掉空 Attr 跳过、去掉slices.Clone、忽略record.PC都会使测试失败。改动文件
提交