Skip to content

fix(os/glog): glog 的 JsonHandler 输出可以导入 ES 的纯 Json (#4790)#4791

Open
yangyanqing wants to merge 2 commits into
gogf:masterfrom
yangyanqing:yangyanqing/4790-glog-with-purl-json
Open

fix(os/glog): glog 的 JsonHandler 输出可以导入 ES 的纯 Json (#4790)#4791
yangyanqing wants to merge 2 commits into
gogf:masterfrom
yangyanqing:yangyanqing/4790-glog-with-purl-json

Conversation

@yangyanqing

@yangyanqing yangyanqing commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #4790

修改要点

  1. 在 ctx 中遍历 CtxKeys 中的 key,如果存在则作为 json 的键值对
  2. 用户输入仍根据类型区分
    • json 文本,打平到 json 中
    • 非 json 文本,输出到 Content 字段中

核心:输出满足 jsonl 格式,能直接导入到 ES、LTS 等工具中分析

范例

func Test_Json(t *testing.T) {
	logger := glog.New()
	logger.SetHandlers(glog.HandlerJson)
	logger.SetCtxKeys("Env", "ClientIP")
	logger.Stack(true)

	ctx := context.WithValue(context.Background(), "Env", "prod")
	ctx = context.WithValue(ctx, "ClientIP", "1.2.3.4")
	logger.Info(ctx, g.Map{"uid": 100, "name": "john"})
	logger.Errorf(ctx, "error occurred, %d", 100200)
}
{"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"}

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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 HandlerJson to 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)
}
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.

os/glog: 支持纯 json 输出

3 participants