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

ginplus.go line35 冗余代码 #63

Closed
pwli0755 opened this issue Dec 27, 2019 · 2 comments
Closed

ginplus.go line35 冗余代码 #63

pwli0755 opened this issue Dec 27, 2019 · 2 comments

Comments

@pwli0755
Copy link

pwli0755 commented Dec 27, 2019

ginplus.go line35 parent = logger.NewTraceIDContext(parent, GetTraceID(c)) 中的 GetTraceID(c)这次调用为冗余操作

func NewContext(c *gin.Context) context.Context {
	parent := context.Background()

	if v := GetTraceID(c); v != "" {
		parent = icontext.NewTraceID(parent, v)
		parent = logger.NewTraceIDContext(parent, GetTraceID(c))
	}

	if v := GetUserID(c); v != "" {
		parent = icontext.NewUserID(parent, v)
		parent = logger.NewUserIDContext(parent, v)
	}

	return parent
}

首先感谢作者开源;另外,想请教下为什么这里要这么多层ctx(在只有traceID和userID且均为已知的情况下)?

@dyzsoft
Copy link

dyzsoft commented Dec 27, 2019

代码看的真仔细啊,同为为啥这么多层的ctx。。

@LyricTian
Copy link
Owner

已经修复👍。对于较为独立的模块来说,模块内应该拥有属于自己的上下文数据封装。在这里logger是作为独立模块引用的,所以要想在logger内使用相关业务数据,就需要在业务入口进行数据注入。

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

No branches or pull requests

3 participants