fix(os/glog): glog 的 JsonHandler 输出可以导入 ES 的纯 Json (#4790)#4791
Open
yangyanqing wants to merge 2 commits into
Open
fix(os/glog): glog 的 JsonHandler 输出可以导入 ES 的纯 Json (#4790)#4791yangyanqing wants to merge 2 commits into
yangyanqing wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates GoFrame’s os/glog JSON handler to emit flat, single-line JSON (JSONL) records that can be directly ingested by log analysis tools (e.g., Elasticsearch/LTS), by (1) exporting configured context keys as top-level JSON fields and (2) flattening JSON-object payloads into the log record instead of embedding them as escaped strings.
Changes:
- Reworked
HandlerJsonto build a dynamic JSON object, merging context-key values and flattening JSON-object content. - Added/updated unit tests to validate flattened JSON output, plain string content handling, and collision behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
os/glog/glog_logger_handler_json.go |
Replaces struct-based JSON output with map-based output, adds ctx-key export + JSON flattening logic. |
os/glog/glog_z_unit_logger_handler_test.go |
Updates/adds unit tests to assert JSON handler emits ES-importable flat JSON and handles collisions/content types. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+24
to
+28
| output.Set("Level", in.LevelFormat) | ||
| setJsonHandlerOutputValue(output, "CallerFunc", in.CallerFunc) | ||
| setJsonHandlerOutputValue(output, "CallerPath", in.CallerPath) | ||
| setJsonHandlerOutputContent(output, in) | ||
| setJsonHandlerOutputValue(output, "Stack", in.Stack) |
Comment on lines
+48
to
+61
| func setJsonHandlerOutputCtxValues(ctx context.Context, in *HandlerInput, output *gmap.StrAnyMap) { | ||
| if ctx == nil || in.Logger == nil { | ||
| return | ||
| } | ||
| for _, ctxKey := range in.Logger.GetCtxKeys() { | ||
| ctxValue := ctx.Value(ctxKey) | ||
| if ctxValue == nil { | ||
| ctxValue = ctx.Value(gctx.StrKey(gconv.String(ctxKey))) | ||
| } | ||
| if ctxValue != nil { | ||
| output.Set(gconv.String(ctxKey), ctxValue) | ||
| } | ||
| } | ||
| } |
Comment on lines
+73
to
+83
| var contentMap map[string]any | ||
| if content != "" && json.Unmarshal([]byte(content), &contentMap) == nil { | ||
| for key, value := range contentMap { | ||
| if _, found := output.Search(key); !found { | ||
| output.Set(key, value) | ||
| } | ||
| } | ||
| return | ||
| } | ||
| output.Set("Content", content) | ||
| } |
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.
Fixes #4790
修改要点
核心:输出满足 jsonl 格式,能直接导入到 ES、LTS 等工具中分析
范例
{"ClientIP":"1.2.3.4","Env":"prod","Level":"INFO","Time":"2026-07-02T10:28:08.490+08:00","name":"john","uid":100} {"ClientIP":"1.2.3.4","Content":"error occurred, 100200","Env":"prod","Level":"ERRO","Time":"2026-07-02T10:28:08.490+08:00"}