Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lixianpei opened this issue Sep 26, 2024 · 8 comments
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@lixianpei
Copy link

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的值

@lixianpei lixianpei added the bug It is confirmed a bug, but don't worry, we'll handle it. label Sep 26, 2024
@Issues-translate-bot Issues-translate-bot changed the title os/gtime: issue title looger 日志组件无法记录自定义的上下文key os/gtime: issue title looger The log component cannot record the custom context key Sep 26, 2024
@JimDevil
Copy link
Contributor

哥们,不知道你说的是这样么?
image
可以把完整代码贴一下

@helloteemo
Copy link
Contributor

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

CC@JimDevil

@Issues-translate-bot
Copy link

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
Copy link
Contributor

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

CC@JimDevil

yeah,you are right!

@helloteemo
Copy link
Contributor

helloteemo commented Sep 26, 2024

@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
Copy link
Author

lixianpei commented Sep 26, 2024

配置:
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
Copy link
Contributor

@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
Copy link
Author

@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~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
Development

No branches or pull requests

4 participants