go-zero/core/logx/tracelogger_test.go

111 lines
3.3 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package logx
import (
"context"
2020-10-07 22:54:51 +08:00
"log"
2020-07-26 17:09:05 +08:00
"strings"
"testing"
2020-10-07 22:54:51 +08:00
"time"
2020-07-26 17:09:05 +08:00
"github.com/stretchr/testify/assert"
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/core/trace/tracespec"
2020-07-26 17:09:05 +08:00
)
const (
mockTraceId = "mock-trace-id"
mockSpanId = "mock-span-id"
)
var mock tracespec.Trace = new(mockTrace)
func TestTraceLog(t *testing.T) {
2020-10-09 14:15:27 +08:00
var buf mockWriter
2020-07-26 17:09:05 +08:00
ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
WithContext(ctx).(*traceLogger).write(&buf, levelInfo, testlog)
2020-07-26 17:09:05 +08:00
assert.True(t, strings.Contains(buf.String(), mockTraceId))
assert.True(t, strings.Contains(buf.String(), mockSpanId))
}
2020-10-07 22:54:51 +08:00
func TestTraceError(t *testing.T) {
2020-10-09 14:15:27 +08:00
var buf mockWriter
2020-10-07 22:54:51 +08:00
ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
l := WithContext(ctx).(*traceLogger)
SetLevel(InfoLevel)
errorLog = newLogWriter(log.New(&buf, "", flags))
l.WithDuration(time.Second).Error(testlog)
assert.True(t, strings.Contains(buf.String(), mockTraceId))
assert.True(t, strings.Contains(buf.String(), mockSpanId))
buf.Reset()
l.WithDuration(time.Second).Errorf(testlog)
assert.True(t, strings.Contains(buf.String(), mockTraceId))
assert.True(t, strings.Contains(buf.String(), mockSpanId))
}
func TestTraceInfo(t *testing.T) {
2020-10-09 14:15:27 +08:00
var buf mockWriter
2020-10-07 22:54:51 +08:00
ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
l := WithContext(ctx).(*traceLogger)
SetLevel(InfoLevel)
infoLog = newLogWriter(log.New(&buf, "", flags))
l.WithDuration(time.Second).Info(testlog)
assert.True(t, strings.Contains(buf.String(), mockTraceId))
assert.True(t, strings.Contains(buf.String(), mockSpanId))
buf.Reset()
l.WithDuration(time.Second).Infof(testlog)
assert.True(t, strings.Contains(buf.String(), mockTraceId))
assert.True(t, strings.Contains(buf.String(), mockSpanId))
}
func TestTraceSlow(t *testing.T) {
2020-10-09 14:15:27 +08:00
var buf mockWriter
2020-10-07 22:54:51 +08:00
ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
l := WithContext(ctx).(*traceLogger)
SetLevel(InfoLevel)
slowLog = newLogWriter(log.New(&buf, "", flags))
l.WithDuration(time.Second).Slow(testlog)
assert.True(t, strings.Contains(buf.String(), mockTraceId))
assert.True(t, strings.Contains(buf.String(), mockSpanId))
buf.Reset()
l.WithDuration(time.Second).Slowf(testlog)
assert.True(t, strings.Contains(buf.String(), mockTraceId))
assert.True(t, strings.Contains(buf.String(), mockSpanId))
}
func TestTraceWithoutContext(t *testing.T) {
2020-10-09 14:15:27 +08:00
var buf mockWriter
2020-10-07 22:54:51 +08:00
l := WithContext(context.Background()).(*traceLogger)
SetLevel(InfoLevel)
infoLog = newLogWriter(log.New(&buf, "", flags))
l.WithDuration(time.Second).Info(testlog)
assert.False(t, strings.Contains(buf.String(), mockTraceId))
assert.False(t, strings.Contains(buf.String(), mockSpanId))
buf.Reset()
l.WithDuration(time.Second).Infof(testlog)
assert.False(t, strings.Contains(buf.String(), mockTraceId))
assert.False(t, strings.Contains(buf.String(), mockSpanId))
}
2020-07-26 17:09:05 +08:00
type mockTrace struct{}
func (t mockTrace) TraceId() string {
return mockTraceId
}
func (t mockTrace) SpanId() string {
return mockSpanId
}
func (t mockTrace) Finish() {
}
func (t mockTrace) Fork(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
return nil, nil
}
func (t mockTrace) Follow(ctx context.Context, serviceName, operationName string) (context.Context, tracespec.Trace) {
return nil, nil
}
func (t mockTrace) Visit(fn func(key string, val string) bool) {
}