gogf / gf

GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang.
https://goframe.org
MIT License
11.79k stars 1.61k forks source link

os/gtime: issue title looger The log component cannot record the custom context key #3813

Closed lixianpei closed 2 months ago

lixianpei commented 2 months ago

Go version

go 1.22

GoFrame version

v2.7.2

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

type CtxCrontabSnKeyType string

const CtxCrontabSnKey CtxCrontabSnKeyType = "crontabSn"

【异常】定义一个上下文,自定义上下文key,希望能够在log文件中同时打印这个字符串“ABC”

ctx = context.WithValue(c, CtxCrontabSnKey, "ABC")

【正确】直接使用字符串则是正确的

ctx = context.WithValue(c, "crontabSn", "ABC")

What did you see happen?

在log文件中无法正常打印自定义的上下文key=crontabSn的值

What did you expect to see?

即使使用别名定义的key也能在log文件中正常打印自定义的上下文key=crontabSn的值

JimDevil commented 2 months ago

哥们,不知道你说的是这样么?

image

可以把完整代码贴一下

helloteemo commented 2 months ago

@lixianpei 不能自定义类型,否则ctx.Value无法读取到你的值

CC@JimDevil

Issues-translate-bot commented 2 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@lixianpei You cannot customize the type, otherwise ctx.Value cannot read your value

CC@JimDevil

JimDevil commented 2 months ago

@lixianpei 不能自定义类型,否则ctx.Value无法读取到你的值

CC@JimDevil

yeah,you are right!

helloteemo commented 2 months ago

@lixianpei can be used like this:

const CtxCrontabSnKey gctx.StrKey = "crontabSn"

The gf source code is as follows:

//  The key point here is this part, the default type expected is gctx.StrKey. So using gctx.StrKey is most appropriate
ctxValue = ctx.Value(gctx.StrKey(gconv.String(ctxKey)))

So I don't think this is a bug, but it can definitely be modified in the document.

CC @JimDevil

lixianpei commented 2 months ago

配置: crontabTask: CtxKeys: ["crontabArgs", "batchSn"]

` package main

import ( "context" "github.com/gogf/gf/contrib/drivers/mysql/v2" "github.com/gogf/gf/contrib/nosql/redis/v2" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "server/internal/logic" "server/internal/packed" )

type BatchSnType string

const BatchSn BatchSnType = "batchSn"

func main() { ctx := context.WithValue(gctx.New(), "crontabArgs", "A") ctx = context.WithValue(ctx, BatchSn, "B") g.Log("crontabTask").Info(ctx, "测试一下") }

`

日志内容:只有A记录了,B没有记录(似乎无法直接用别名) 2024-09-26 11:28:51.590 [INFO] {28f1526e99adf81726db6d6d788ac3b1} {A} 测试一下

helloteemo commented 2 months ago

@lixianpei Yes, aliases cannot be used, the reason has been explained above, and the official documentation of gf has been updated.

https://goframe.org/pages/viewpage.action?pageId=1114222

image
lixianpei commented 2 months ago

@lixianpei can be used like this:

const CtxCrontabSnKey gctx.StrKey = "crontabSn"

The gf source code is as follows:

//  The key point here is this part, the default type expected is gctx.StrKey. So using gctx.StrKey is most appropriate
ctxValue = ctx.Value(gctx.StrKey(gconv.String(ctxKey)))

So I don't think this is a bug, but it can definitely be modified in the document.

CC @JimDevil

thanks~