Kafka日志在Go服务中

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

Kafka logs in go service

问题

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


	chanLogs := make(chan confluentkafka.LogEvent)
	go func() {
		for {
			logEv := <-chanLogs
			logger.Debug("KAFKA: " + logEv.String())
		}
	}()

	configMap["go.logs.channel.enable"] = true
	configMap["go.logs.channel"] = chanLogs

	consumer, err := confluentkafka.NewConsumer(&configMap)

	err := consumer.SubscribeTopics(Topics, nil)

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

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

英文:

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


	chanLogs := make(chan confluentkafka.LogEvent)
	go func() {
		for {
			logEv := &lt;-chanLogs
			logger.Debug(&quot;KAFKA: &quot; + logEv.String())
		}
	}()

	configMap[&quot;go.logs.channel.enable&quot;] = true
	configMap[&quot;go.logs.channel&quot;] = chanLogs

	consumer, err := confluentkafka.NewConsumer(&amp;configMap)

	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

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

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

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

    configMap["go.logs.channel"] = chanLogs
    configMap["go.logs.channel.enable"] = true

    consumer, err := confluentkafka.NewConsumer(&configMap)

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

英文:

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

go.logs.channel.enable (bool, false) - Forward log to Logs() channel.
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:

    configMap[&quot;go.logs.channel&quot;] = chanLogs
    configMap[&quot;go.logs.channel.enable&quot;] = true

    consumer, err := confluentkafka.NewConsumer(&amp;configMap)

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

答案2

得分: 1

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

    configMap["debug"] = "all"

我在这里找到了它:链接

英文:

The solution was to add

    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:

确定