|
|
|
@ -2,8 +2,10 @@ package logx
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"log"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/tal-tech/go-zero/core/trace/tracespec"
|
|
|
|
@ -24,6 +26,65 @@ func TestTraceLog(t *testing.T) {
|
|
|
|
|
assert.True(t, strings.Contains(buf.String(), mockSpanId))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTraceError(t *testing.T) {
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
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) {
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
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) {
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
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) {
|
|
|
|
|
var buf strings.Builder
|
|
|
|
|
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))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type mockTrace struct{}
|
|
|
|
|
|
|
|
|
|
func (t mockTrace) TraceId() string {
|
|
|
|
|