代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。