1 Star 0 Fork 0

gaozhenhai/log

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
test_sink.go 1.31 KB
一键复制 编辑 原始数据 按行查看 历史
gaozhenhai 提交于 2020-04-23 17:10 . init code
package lagertest
import (
"bytes"
"encoding/json"
"io"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega/gbytes"
"gitee.com/gaozhenhai/log/lager"
)
type TestLogger struct {
lager.Logger
*TestSink
}
type TestSink struct {
lager.Sink
buffer *gbytes.Buffer
}
func NewTestLogger(component string) *TestLogger {
logger := lager.NewLogger(component)
testSink := NewTestSink()
logger.RegisterSink(testSink)
logger.RegisterSink(lager.NewWriterSink(ginkgo.GinkgoWriter, lager.DEBUG))
return &TestLogger{logger, testSink}
}
func NewTestSink() *TestSink {
buffer := gbytes.NewBuffer()
return &TestSink{
Sink: lager.NewWriterSink(buffer, lager.DEBUG),
buffer: buffer,
}
}
func (s *TestSink) Buffer() *gbytes.Buffer {
return s.buffer
}
func (s *TestSink) Logs() []lager.LogFormat {
logs := []lager.LogFormat{}
var err error
decoder := json.NewDecoder(bytes.NewBuffer(s.buffer.Contents()))
for {
var log lager.LogFormat
if err = decoder.Decode(&log); err == io.EOF {
return logs
} else if err != nil {
break
//panic(err)
}
logs = append(logs, log)
}
if err != nil {
panic(err)
}
return logs
}
func (s *TestSink) LogMessages() []string {
logs := s.Logs()
messages := make([]string, 0, len(logs))
for _, log := range logs {
messages = append(messages, log.Message)
}
return messages
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/gaozhenhai/log.git
git@gitee.com:gaozhenhai/log.git
gaozhenhai
log
log
7e68a9fe4ebd

搜索帮助