go-zero/core/logx/tracelogger_test.go

117 lines
3.5 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"
2020-10-09 19:13:10 +08:00
"sync/atomic"
2020-07-26 17:09:05 +08:00
"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 (
2021-02-20 22:45:58 +08:00
mockTraceID = "mock-trace-id"
mockSpanID = "mock-span-id"
2020-07-26 17:09:05 +08:00
)
var mock tracespec.Trace = new(mockTrace)
func TestTraceLog(t *testing.T) {
2020-10-09 14:15:27 +08:00
var buf mockWriter
atomic.StoreUint32(&initialized, 1)
2020-07-26 17:09:05 +08:00
ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
WithContext(ctx).(*traceLogger).write(&buf, levelInfo, testlog)
2021-02-20 22:45:58 +08:00
assert.True(t, strings.Contains(buf.String(), mockTraceID))
assert.True(t, strings.Contains(buf.String(), mockSpanID))
2020-07-26 17:09:05 +08:00
}
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-10 15:36:07 +08:00
atomic.StoreUint32(&initialized, 1)
errorLog = newLogWriter(log.New(&buf, "", flags))
2020-10-07 22:54:51 +08:00
ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
l := WithContext(ctx).(*traceLogger)
SetLevel(InfoLevel)
l.WithDuration(time.Second).Error(testlog)
2021-02-20 22:45:58 +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
buf.Reset()
l.WithDuration(time.Second).Errorf(testlog)
2021-02-20 22:45:58 +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 TestTraceInfo(t *testing.T) {
2020-10-09 14:15:27 +08:00
var buf mockWriter
2020-10-10 15:36:07 +08:00
atomic.StoreUint32(&initialized, 1)
infoLog = newLogWriter(log.New(&buf, "", flags))
2020-10-07 22:54:51 +08:00
ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
l := WithContext(ctx).(*traceLogger)
SetLevel(InfoLevel)
l.WithDuration(time.Second).Info(testlog)
2021-02-20 22:45:58 +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
buf.Reset()
l.WithDuration(time.Second).Infof(testlog)
2021-02-20 22:45:58 +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 TestTraceSlow(t *testing.T) {
2020-10-09 14:15:27 +08:00
var buf mockWriter
2020-10-10 15:36:07 +08:00
atomic.StoreUint32(&initialized, 1)
slowLog = newLogWriter(log.New(&buf, "", flags))
2020-10-07 22:54:51 +08:00
ctx := context.WithValue(context.Background(), tracespec.TracingKey, mock)
l := WithContext(ctx).(*traceLogger)
SetLevel(InfoLevel)
l.WithDuration(time.Second).Slow(testlog)
2021-02-20 22:45:58 +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
buf.Reset()
l.WithDuration(time.Second).Slowf(testlog)
2021-02-20 22:45:58 +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 TestTraceWithoutContext(t *testing.T) {
2020-10-09 14:15:27 +08:00
var buf mockWriter
2020-10-09 19:13:10 +08:00
atomic.StoreUint32(&initialized, 1)
2020-10-07 22:54:51 +08:00
infoLog = newLogWriter(log.New(&buf, "", flags))
2020-10-10 15:36:07 +08:00
l := WithContext(context.Background()).(*traceLogger)
SetLevel(InfoLevel)
2020-10-07 22:54:51 +08:00
l.WithDuration(time.Second).Info(testlog)
2021-02-20 22:45:58 +08:00
assert.False(t, strings.Contains(buf.String(), mockTraceID))
assert.False(t, strings.Contains(buf.String(), mockSpanID))
2020-10-07 22:54:51 +08:00
buf.Reset()
l.WithDuration(time.Second).Infof(testlog)
2021-02-20 22:45:58 +08:00
assert.False(t, strings.Contains(buf.String(), mockTraceID))
assert.False(t, strings.Contains(buf.String(), mockSpanID))
2020-10-07 22:54:51 +08:00
}
2020-07-26 17:09:05 +08:00
type mockTrace struct{}
func (t mockTrace) TraceId() string {
2021-02-20 22:45:58 +08:00
return mockTraceID
2020-07-26 17:09:05 +08:00
}
func (t mockTrace) SpanId() string {
2021-02-20 22:45:58 +08:00
return mockSpanID
2020-07-26 17:09:05 +08:00
}
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, val string) bool) {
2020-07-26 17:09:05 +08:00
}