[英]How to split log file in using glog(github.com/golang/glog)
[英]Testing log output from GitHub.com/Shopify/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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.