add caller info to the testHook

Signed-off-by: Iceber Gu <wei.cai-nat@daocloud.io>
This commit is contained in:
Iceber Gu 2021-02-27 03:49:05 +08:00
parent 56f17a0856
commit bf9db47e8a
No known key found for this signature in database
GPG Key ID: F1632F7124070DD4

View File

@ -18,7 +18,10 @@ package logtest
import (
"context"
"fmt"
"io/ioutil"
"path/filepath"
"runtime"
"testing"
"github.com/containerd/containerd/log"
@ -35,6 +38,7 @@ func WithT(ctx context.Context, t testing.TB) context.Context {
// Increase debug level for tests
l.SetLevel(logrus.DebugLevel)
l.SetOutput(ioutil.Discard)
l.SetReportCaller(true)
// Add testing hook
l.AddHook(&testHook{
@ -42,6 +46,9 @@ func WithT(ctx context.Context, t testing.TB) context.Context {
fmt: &logrus.TextFormatter{
DisableColors: true,
TimestampFormat: log.RFC3339NanoFixed,
CallerPrettyfier: func(frame *runtime.Frame) (string, string) {
return filepath.Base(frame.Function), fmt.Sprintf("%s:%d", frame.File, frame.Line)
},
},
})