kratos icon indicating copy to clipboard operation
kratos copied to clipboard

[Feature] logHelper是否可以增加with的功能?

Open shcw opened this issue 2 years ago • 7 comments

Feature-request

当前的log只能在log.logger时增加全局信息,而有时执行的过程中才会增加局部的全局变量,则需要使用logger而不是helper,就会很麻烦。

Proposal

helper := log.NewHelper() helper = helper.With(helper, "k1", "v1", "k2", "v2");

shcw avatar Sep 01 '22 04:09 shcw

helper 的 log 方法无 error,logger的 log 方法 有 error ,是不能这么做的原因。但目前没发现 error有啥用。

hrygo avatar Sep 01 '22 09:09 hrygo

感觉helper添加一个with还蛮有用的 这样在biz层可以直接使用hepler了

shcw avatar Sep 01 '22 09:09 shcw

从biz层传递日志field到data层有什么好的方法吗?每个data层的接口都加一个logger参数?

windzhu0514 avatar Sep 17 '22 03:09 windzhu0514

从biz层传递日志field到data层有什么好的方法吗?每个data层的接口都加一个logger参数?

我目前是每次新增加一个 😂😂😂😂

shcw avatar Oct 08 '22 10:10 shcw

之前也是使用创建data的时候传进去的logger新建一个helper,现在把logger放到context里传递

windzhu0514 avatar Oct 08 '22 10:10 windzhu0514

之前也是使用创建data的时候传进去的logger新建一个helper,现在把logger放到context里传递

老哥给个代码例子看看呗

shcw avatar Oct 08 '22 10:10 shcw

之前也是使用创建data的时候传进去的logger新建一个helper,现在把logger放到context里传递

老哥给个代码例子看看呗

package logx

import (
	"context"

	"github.com/go-kratos/kratos/v2/log"
)

type loggerContextKey struct{}

func NewContext(ctx context.Context, logger log.Logger) context.Context {
	return context.WithValue(ctx, loggerContextKey{}, logger)
}

func FromContext(ctx context.Context) log.Logger {
	if v, ok := ctx.Value(loggerContextKey{}).(log.Logger); ok {
		return v
	}

	return log.GetLogger()
}

golang官方也在讨论日志怎么传递

https://github.com/golang/go/discussions/54763

windzhu0514 avatar Oct 08 '22 10:10 windzhu0514

用了一段时间自带的日志,感觉用zap的更方便一点

windzhu0514 avatar Jan 31 '23 09:01 windzhu0514

用了一段时间自带的日志,感觉用zap的更方便一点

如果都切走的话成本会不会有点高

shcw avatar Jan 31 '23 11:01 shcw

用了一段时间自带的日志,感觉用zap的更方便一点

如果都切走的话成本会不会有点高

代码里所有的日志替换为zap.Logger或者zap.SugaredLogger,程序启动的把zap logger注入到框架默认的全局日志

windzhu0514 avatar Feb 01 '23 02:02 windzhu0514

这个issue没人看吗? 我觉得这个局部logger field还是很常见的需求

tarupo avatar Mar 20 '23 10:03 tarupo

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


Has no one read this issue? I think this partial logger field is still a very common requirement

kratos-ci-bot avatar Mar 20 '23 10:03 kratos-ci-bot

Hi, @shcw! I'm Dosu, and I'm helping the kratos team manage their backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, you requested a feature to add the ability to include local global variables in the log using the logHelper. There has been some discussion about the usefulness of this feature and alternative solutions, such as using zap.Logger. However, the issue is still open and awaiting further input from the maintainers.

Could you please let us know if this issue is still relevant to the latest version of the kratos repository? If it is, please comment on the issue to let us know. Otherwise, feel free to close the issue yourself or it will be automatically closed in 7 days.

Thank you for your understanding and contribution to the kratos project!

dosubot[bot] avatar Nov 03 '23 16:11 dosubot[bot]

Hi, @shcw! I'm Dosu, and I'm helping the kratos team manage their backlog. I wanted to let you know that we are marking this issue as stale.嗨,@shcw!我是 Dosu,我正在帮助 kratos 团队管理他们的积压工作。我想让您知道我们已将此问题标记为过时。

From what I understand, you requested a feature to add the ability to include local global variables in the log using the logHelper. There has been some discussion about the usefulness of this feature and alternative solutions, such as using zap.Logger. However, the issue is still open and awaiting further input from the maintainers.据我了解,您请求一项功能来添加使用 logHelper 在日志中包含局部全局变量的功能。关于此功能的实用性和替代解决方案(例如使用 zap.Logger)已经有一些讨论。然而,该问题仍然悬而未决,等待维护者的进一步意见。

Could you please let us know if this issue is still relevant to the latest version of the kratos repository? If it is, please comment on the issue to let us know. Otherwise, feel free to close the issue yourself or it will be automatically closed in 7 days.您能否告诉我们这个问题是否仍然与最新版本的 kratos 存储库相关?如果是,请对此问题发表评论以告知我们。否则,请自行关闭问题,否则该问题将在 7 天内自动关闭。

Thank you for your understanding and contribution to the kratos project!

Yes, I wish there was a better logging component. recommended to use zap instead or use the slog of the standard library.

windzhu0514 avatar Nov 08 '23 02:11 windzhu0514

Thank you for your response, @windzhu0514! We appreciate your understanding and contribution to the Kratos project. Based on your feedback, it seems that using zap or the slog of the standard library would be better alternatives for logging. We will proceed to close this issue.

dosubot[bot] avatar Nov 08 '23 02:11 dosubot[bot]