go - 测试来自GitHub.com/Shopify/sarama的日志输出

标签 go sarama

我正在尝试为配置 github.com/Shopify/sarama Logger的功能选项编写单元测试。像这样用Kafka运行Docker容器后,

docker run -p 2181:2181 -p 9092:9092 -e ADVERTISED_HOST=127.0.0.1  -e NUM_PARTITIONS=10 johnnypark/kafka-zookeeper
我正在尝试运行此程序:
package main

import (
    "bufio"
    "bytes"
    "io/ioutil"
    "log"

    "github.com/Shopify/sarama"
)

func main() {
    var b bytes.Buffer
    out := bufio.NewWriter(&b)

    sarama.Logger = log.New(out, "[Sarama] ", log.LstdFlags)

    if _, err := sarama.NewClient([]string{"localhost:9092"}, sarama.NewConfig()); err != nil {
        log.Fatalln("NewClient:", err)
    }

    output, err := ioutil.ReadAll(&b)
    if err != nil {
        log.Fatalln("ReadAll:", err)
    }

    log.Printf("output: %s", output)
}
我希望看到一些输出。但是,打印的输出为空:
> go run main.go
2020/09/25 16:44:58 output: 
相比之下,如果我将输出设置为os.Stderr
package main

import (
    "log"
    "os"

    "github.com/Shopify/sarama"
)

func main() {
    sarama.Logger = log.New(os.Stderr, "[Sarama] ", log.LstdFlags)

    if _, err := sarama.NewClient([]string{"localhost:9092"}, sarama.NewConfig()); err != nil {
        log.Fatalln("NewClient:", err)
    }
}
我看到预期的输出打印到终端:
> go run main.go
[Sarama] 2020/09/25 16:46:04 Initializing new client
[Sarama] 2020/09/25 16:46:04 ClientID is the default of 'sarama', you should consider setting it to something application-specific.
[Sarama] 2020/09/25 16:46:04 ClientID is the default of 'sarama', you should consider setting it to something application-specific.
[Sarama] 2020/09/25 16:46:04 client/metadata fetching metadata for all topics from broker localhost:9092
[Sarama] 2020/09/25 16:46:04 Connected to broker at localhost:9092 (unregistered)
[Sarama] 2020/09/25 16:46:04 client/brokers registered new broker #0 at 127.0.0.1:9092
[Sarama] 2020/09/25 16:46:04 Successfully initialized new client
看来*bytes.Buffer不会被ioutil.ReadAll()冲掉?如何修复前面的示例,以便output为非空?

最佳答案

原来我只需要打电话

out.Flush()
ioutil.ReadAll()之前。现在输出是预期的:
> go run main.go
2020/09/25 16:58:26 output: [Sarama] 2020/09/25 16:58:26 Initializing new client
[Sarama] 2020/09/25 16:58:26 ClientID is the default of 'sarama', you should consider setting it to something application-specific.
[Sarama] 2020/09/25 16:58:26 ClientID is the default of 'sarama', you should consider setting it to something application-specific.
[Sarama] 2020/09/25 16:58:26 client/metadata fetching metadata for all topics from broker localhost:9092
[Sarama] 2020/09/25 16:58:26 Connected to broker at localhost:9092 (unregistered)
[Sarama] 2020/09/25 16:58:26 client/brokers registered new broker #0 at 127.0.0.1:9092
[Sarama] 2020/09/25 16:58:26 Successfully initialized new client

关于go - 测试来自GitHub.com/Shopify/sarama的日志输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64072565/

相关文章:

mongodb - 创建 session : no reachable servers - mgo

go - 如何解决从openapi go代码生成器获取使用应用程序/x-www-form-urlencoded内容类型的API的验证错误?

go - 为什么 Shopify Sarama 消费者需要分区来消费消息

go - 如何在 Go 中正确测试 Controller 类

string - 在 Golang 中将 []interface 转换为 []string

go - Go for protobuf 中的相对导入,找不到模块路径

go - Golang Consumer连接Kafka后延迟接收Kafka消息

json - 将 JSON 对象数组解析为单个文档

go - 无法使用来自本地运行的 Kafka 服务器的消息,使用 Golang Sarama 包

go - 重命名Sarama的kafka消费群?