Kafka日志在Go服务中

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

Kafka logs in go service

问题

我需要用于调试的Kafka消费者日志。我按照以下步骤进行操作:

  1. chanLogs := make(chan confluentkafka.LogEvent)
  2. go func() {
  3. for {
  4. logEv := <-chanLogs
  5. logger.Debug("KAFKA: " + logEv.String())
  6. }
  7. }()
  8. configMap["go.logs.channel.enable"] = true
  9. configMap["go.logs.channel"] = chanLogs
  10. consumer, err := confluentkafka.NewConsumer(&configMap)
  11. err := consumer.SubscribeTopics(Topics, nil)

但是我从来没有得到过一行日志。我尝试使用kafka chan (consumer.Logs()),结果也是一样。我做错了什么?

更新
在初始帖子中,我错误地设置了参数名称。正确的参数名称是go.logs.channel.enable。但有时这仍然不起作用。

英文:

I need kafka consumer logs for debug. I do the following:

  1. chanLogs := make(chan confluentkafka.LogEvent)
  2. go func() {
  3. for {
  4. logEv := &lt;-chanLogs
  5. logger.Debug(&quot;KAFKA: &quot; + logEv.String())
  6. }
  7. }()
  8. configMap[&quot;go.logs.channel.enable&quot;] = true
  9. configMap[&quot;go.logs.channel&quot;] = chanLogs
  10. consumer, err := confluentkafka.NewConsumer(&amp;configMap)
  11. err := consumer.SubscribeTopics(Topics, nil)

And I never get a line. I tried it with kafka chan (consumer.Logs()) with the same result. What I do wrong?

UPD
In initial post I wrongfully set parameter name. The correct one is go.logs.channel.enable. But sometimes this still don't work.

答案1

得分: 1

根据文档的描述,你应该启用该功能:

  1. go.logs.channel (chan kafka.LogEvent, nil) - 将日志转发到应用程序提供的通道,而不是 Logs()。需要设置 go.logs.channel.enable=true

所以你需要修改你的代码如下:

  1. configMap["go.logs.channel"] = chanLogs
  2. configMap["go.logs.channel.enable"] = true
  3. consumer, err := confluentkafka.NewConsumer(&configMap)

文档或代码仓库的示例这里也可以找到更多信息。

英文:

As described in the doc, you should enable that feature:

  1. go.logs.channel.enable (bool, false) - Forward log to Logs() channel.
  2. go.logs.channel (chan kafka.LogEvent, nil) - Forward logs to application-provided channel instead of Logs(). Requires go.logs.channel.enable=true.

So change your code like:

  1. configMap[&quot;go.logs.channel&quot;] = chanLogs
  2. configMap[&quot;go.logs.channel.enable&quot;] = true
  3. consumer, err := confluentkafka.NewConsumer(&amp;configMap)

See also in the doc or in the sample on the code repo here

答案2

得分: 1

解决方案是添加以下代码:

  1. configMap["debug"] = "all"

我在这里找到了它:链接

英文:

The solution was to add

  1. configMap[&quot;debug&quot;] = &quot;all&quot;

I found it here

huangapple
  • 本文由 发表于 2021年11月23日 15:23:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/70076838.html
匿名

发表评论

匿名网友

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

确定