如何使用省略号来禁用全局日志记录器

huangapple go评论73阅读模式
英文:

How to use ellipsis for zap global logger

问题

你好,我正在尝试将"sugar logger"更改为"global longer",我检查了可能使用的字段,但是没有找到解决我的问题的方法。

例如,在某些情况下,我使用以下代码:

zap.L().Debug("recv_cmd",
	zap.String("user", c.GetString("user")),
	zap.String("path", c.Request.URL.Path),
)

这是我在大多数情况下使用的方法,但是我有一个不同的情况,类似于这样:

params := make([]interface{}, 0, 20)
params = append(params,
	"status", c.Writer.Status(),
	"method", c.Request.Method,
	"path", c.Request.URL.Path,
	"ip", c.ClientIP(),
)
if len(body) > 0 {
	params = append(params, "body", string(body))
}

所以在这种情况下,每个请求中都没有body,因此params对象对于每个请求的结构不同。

我想要的是(这只是一个简单的演示,我知道stringer不起作用):

zap.L().Info("Info",
	zap.Stringer("request", params...),
)
英文:

Hello I am trying to change sugar logger to global longer I checked possible fields which I can use but I couldn't something solve my problem

for example in some situations I use

					zap.L().Debug("recv_cmd",
						zap.String("user", c.GetString("user")),
						zap.String("path", c.Request.URL.Path),
					)

This is what I use in most of the cases but I have one different case which like this

    params := make([]interface{}, 0, 20)
	params = append(params,
		"status", c.Writer.Status(),
		"method", c.Request.Method
		"path", c.Request.URL.Path,
		"ip", c.ClientIP(),
	)
	if len(body) > 0 {
		params = append(params, "body", string(body))
	}

so in this case, I don't have a body in every request, so params obj doesn't have the same struck for each request

what I want is (this is just a simple demonstration I know stringer won't work)

	zap.L().Info("Info",
		zap.Stringer("request", params...),
	)

答案1

得分: 0

Logger.Info 的定义如下:

func (log *Logger) Info(msg string, fields ...Field)

zap.Stringer 的定义如下:

func Stringer(key string, val fmt.Stringer) Field

所以你尝试的方式存在一些问题:

  • 你不能将 []interface{} 传递给接受 fmt.Stringer 的函数(因为它不实现该接口)。
  • zap.Stringer 返回一个单独的 Field,而你实际上想要记录多个字段。

我认为你想要实现的是以下代码(playground)(但我不太清楚 params 是为了记录日志还是其他原因):

var params []zap.Field
params = append(params,
	zap.String("status", c.Status),
	zap.String("method", c.Method),
	zap.String("path", c.URL.Path),
	zap.String("ip", c.ClientIP),
)
if len(body) > 0 {
	params = append(params, zap.String("body", string(body)))
}
zap.L().Info("Info", params...)

如果这不是你想要的,请查看 zap 提供的各种编码器(例如 AnyInline 等)。

注意:我对你的结构进行了一些简化(因为你没有包含细节)。如果你包含一个最小可复现示例,回答这类问题会更容易(即你应该定义所使用的结构,但只需要足够说明问题的程度)。

英文:

The definition of Logger.Info is:

func (log *Logger) Info(msg string, fields ...Field)

and the definition of zap.Stringer is:

func Stringer(key string, val fmt.Stringer) Field

So there are a number of issues with what you are attempting:

  • You cannot pass []interface{} to a function that accepts fmt.Stringer (because it does not implement the interface).
  • zap.Stringer returns a single Field whereas you are really trying to log multiple fields.

What I think you are trying to accomplish is (playground) (but I'm not really clear whether params is being created specifically for logging or for another reason):

var params []zap.Field
params = append(params,
	zap.String("status", c.Status),
	zap.String("method", c.Method),
	zap.String("path", c.URL.Path),
	zap.String("ip", c.ClientIP),
)
if len(body) > 0 {
	params = append(params, zap.String("body", string(body)))
}
zap.L().Info("Info", params...)

If that does not do what you are doing take a look at the variety of encoders that zap provides (e.g. Any, Inline etc).

Note: I have simplified your structure somewhat (because you did not include details). It is easier to answer questions like this if you include a minimum reproducible example (i.e. you should define structures used but only to the degree needed to illustrate the issue).

huangapple
  • 本文由 发表于 2021年12月12日 01:53:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/70317624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定