1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
log.go 2.48 KB
一键复制 编辑 原始数据 按行查看 历史
Steffen Siering 提交于 2017-07-31 22:21 . Pipeline cleanups (#4776)
package queuetest
import (
"bufio"
"flag"
"fmt"
"os"
"sync"
"testing"
"github.com/elastic/beats/libbeat/logp"
)
var debug bool
var printLog bool
type TestLogger struct {
t *testing.T
}
func init() {
flag.BoolVar(&debug, "debug", false, "enable test debug log")
flag.BoolVar(&printLog, "debug-print", false, "print test log messages right away")
}
type testLogWriter struct {
t *testing.T
}
func (w *testLogWriter) Write(p []byte) (int, error) {
w.t.Log(string(p))
return len(p), nil
}
func withLogOutput(fn func(*testing.T)) func(*testing.T) {
return func(t *testing.T) {
stderr := os.Stderr
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
defer r.Close()
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
t.Log(line)
if printLog {
stderr.WriteString(line)
stderr.WriteString("\n")
}
}
}()
os.Stderr = w
defer func() {
os.Stderr = stderr
w.Close()
wg.Wait()
}()
level := logp.LOG_INFO
if debug {
level = logp.LOG_DEBUG
}
logp.LogInit(level, "", false, true, []string{"*"})
fn(t)
}
}
// NewTestLogger creates a new logger interface,
// logging via t.Log/t.Logf. If `-debug` is given on command
// line, debug logs will be included.
// Run tests with `-debug-print`, to print log output to console right away.
// This guarantees logs are still written if the test logs are not printed due
// to a panic in the test itself.
//
// Capturing log output using the TestLogger, will make the
// log output correctly display with test test being run.
func NewTestLogger(t *testing.T) *TestLogger {
return &TestLogger{t}
}
func (l *TestLogger) Debug(vs ...interface{}) {
if debug {
l.t.Log(vs...)
print(vs)
}
}
func (l *TestLogger) Info(vs ...interface{}) {
l.t.Log(vs...)
print(vs)
}
func (l *TestLogger) Err(vs ...interface{}) {
l.t.Error(vs...)
print(vs)
}
func (l *TestLogger) Debugf(format string, v ...interface{}) {
if debug {
l.t.Logf(format, v...)
printf(format, v)
}
}
func (l *TestLogger) Infof(format string, v ...interface{}) {
l.t.Logf(format, v...)
printf(format, v)
}
func (l *TestLogger) Errf(format string, v ...interface{}) {
l.t.Errorf(format, v...)
printf(format, v)
}
func print(vs []interface{}) {
if printLog {
fmt.Println(vs...)
}
}
func printf(format string, vs []interface{}) {
if printLog {
fmt.Printf(format, vs...)
if format[len(format)-1] != '\n' {
fmt.Println("")
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v6.1.2

搜索帮助