登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-04-14 20:54:35 274浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《测试来自 GitHub.com/Shopify/sarama 的日志输出》,聊聊,我们一起来看看吧!

问题内容

我正在尝试为配置 github.com/shopify/saramalogger 的功能选项编写单元测试。像这样使用 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

理论要掌握,实操不能落!以上关于《测试来自 GitHub.com/Shopify/sarama 的日志输出》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>