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"
|
2021-10-03 20:53:50 +08:00
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2020-07-26 17:09:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-10-03 20:53:50 +08:00
|
|
|
traceKey = "trace"
|
|
|
|
spanKey = "span"
|
2020-07-26 17:09:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTraceLog(t *testing.T) {
|
2020-10-09 14:15:27 +08:00
|
|
|
var buf mockWriter
|
2020-12-10 17:04:57 +08:00
|
|
|
atomic.StoreUint32(&initialized, 1)
|
2021-10-03 20:53:50 +08:00
|
|
|
otp := otel.GetTracerProvider()
|
|
|
|
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
|
|
|
otel.SetTracerProvider(tp)
|
|
|
|
defer otel.SetTracerProvider(otp)
|
|
|
|
|
|
|
|
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
2020-09-22 17:34:39 +08:00
|
|
|
WithContext(ctx).(*traceLogger).write(&buf, levelInfo, testlog)
|
2021-10-03 20:53:50 +08:00
|
|
|
assert.True(t, strings.Contains(buf.String(), traceKey))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), spanKey))
|
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))
|
2021-10-03 20:53:50 +08:00
|
|
|
otp := otel.GetTracerProvider()
|
|
|
|
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
|
|
|
otel.SetTracerProvider(tp)
|
|
|
|
defer otel.SetTracerProvider(otp)
|
|
|
|
|
|
|
|
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
2020-10-07 22:54:51 +08:00
|
|
|
l := WithContext(ctx).(*traceLogger)
|
|
|
|
SetLevel(InfoLevel)
|
|
|
|
l.WithDuration(time.Second).Error(testlog)
|
2021-10-03 20:53:50 +08:00
|
|
|
assert.True(t, strings.Contains(buf.String(), traceKey))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), spanKey))
|
2020-10-07 22:54:51 +08:00
|
|
|
buf.Reset()
|
|
|
|
l.WithDuration(time.Second).Errorf(testlog)
|
2021-10-03 20:53:50 +08:00
|
|
|
assert.True(t, strings.Contains(buf.String(), traceKey))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), spanKey))
|
2021-12-13 11:58:58 +08:00
|
|
|
buf.Reset()
|
|
|
|
l.WithDuration(time.Second).Errorv(testlog)
|
|
|
|
assert.True(t, strings.Contains(buf.String(), traceKey))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), spanKey))
|
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))
|
2021-10-03 20:53:50 +08:00
|
|
|
otp := otel.GetTracerProvider()
|
|
|
|
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
|
|
|
otel.SetTracerProvider(tp)
|
|
|
|
defer otel.SetTracerProvider(otp)
|
|
|
|
|
|
|
|
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
2020-10-07 22:54:51 +08:00
|
|
|
l := WithContext(ctx).(*traceLogger)
|
|
|
|
SetLevel(InfoLevel)
|
|
|
|
l.WithDuration(time.Second).Info(testlog)
|
2021-10-03 20:53:50 +08:00
|
|
|
assert.True(t, strings.Contains(buf.String(), traceKey))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), spanKey))
|
2020-10-07 22:54:51 +08:00
|
|
|
buf.Reset()
|
|
|
|
l.WithDuration(time.Second).Infof(testlog)
|
2021-10-03 20:53:50 +08:00
|
|
|
assert.True(t, strings.Contains(buf.String(), traceKey))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), spanKey))
|
2021-12-13 11:58:58 +08:00
|
|
|
buf.Reset()
|
|
|
|
l.WithDuration(time.Second).Infov(testlog)
|
|
|
|
assert.True(t, strings.Contains(buf.String(), traceKey))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), spanKey))
|
2020-10-07 22:54:51 +08:00
|
|
|
}
|
|
|
|
|
2022-01-17 12:43:15 +08:00
|
|
|
func TestTraceInfoConsole(t *testing.T) {
|
2022-02-28 23:17:51 +08:00
|
|
|
old := atomic.LoadUint32(&encoding)
|
|
|
|
atomic.StoreUint32(&encoding, jsonEncodingType)
|
2022-01-17 12:43:15 +08:00
|
|
|
defer func() {
|
2022-02-28 23:17:51 +08:00
|
|
|
atomic.StoreUint32(&encoding, old)
|
2022-01-17 12:43:15 +08:00
|
|
|
}()
|
|
|
|
|
|
|
|
var buf mockWriter
|
|
|
|
atomic.StoreUint32(&initialized, 1)
|
|
|
|
infoLog = newLogWriter(log.New(&buf, "", flags))
|
|
|
|
otp := otel.GetTracerProvider()
|
|
|
|
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
|
|
|
otel.SetTracerProvider(tp)
|
|
|
|
defer otel.SetTracerProvider(otp)
|
|
|
|
|
|
|
|
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
|
|
|
l := WithContext(ctx).(*traceLogger)
|
|
|
|
SetLevel(InfoLevel)
|
|
|
|
l.WithDuration(time.Second).Info(testlog)
|
|
|
|
assert.True(t, strings.Contains(buf.String(), traceIdFromContext(ctx)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), spanIdFromContext(ctx)))
|
|
|
|
buf.Reset()
|
|
|
|
l.WithDuration(time.Second).Infof(testlog)
|
|
|
|
assert.True(t, strings.Contains(buf.String(), traceIdFromContext(ctx)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), spanIdFromContext(ctx)))
|
|
|
|
buf.Reset()
|
|
|
|
l.WithDuration(time.Second).Infov(testlog)
|
|
|
|
assert.True(t, strings.Contains(buf.String(), traceIdFromContext(ctx)))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), spanIdFromContext(ctx)))
|
|
|
|
}
|
|
|
|
|
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))
|
2021-10-03 20:53:50 +08:00
|
|
|
otp := otel.GetTracerProvider()
|
|
|
|
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
|
|
|
otel.SetTracerProvider(tp)
|
|
|
|
defer otel.SetTracerProvider(otp)
|
|
|
|
|
|
|
|
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
2020-10-07 22:54:51 +08:00
|
|
|
l := WithContext(ctx).(*traceLogger)
|
|
|
|
SetLevel(InfoLevel)
|
|
|
|
l.WithDuration(time.Second).Slow(testlog)
|
2021-10-03 20:53:50 +08:00
|
|
|
assert.True(t, strings.Contains(buf.String(), traceKey))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), spanKey))
|
2020-10-07 22:54:51 +08:00
|
|
|
buf.Reset()
|
|
|
|
l.WithDuration(time.Second).Slowf(testlog)
|
2021-10-03 20:53:50 +08:00
|
|
|
assert.True(t, strings.Contains(buf.String(), traceKey))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), spanKey))
|
2021-12-13 11:58:58 +08:00
|
|
|
buf.Reset()
|
|
|
|
l.WithDuration(time.Second).Slowv(testlog)
|
|
|
|
assert.True(t, strings.Contains(buf.String(), traceKey))
|
|
|
|
assert.True(t, strings.Contains(buf.String(), spanKey))
|
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-10-03 20:53:50 +08:00
|
|
|
assert.False(t, strings.Contains(buf.String(), traceKey))
|
|
|
|
assert.False(t, strings.Contains(buf.String(), spanKey))
|
2020-10-07 22:54:51 +08:00
|
|
|
buf.Reset()
|
|
|
|
l.WithDuration(time.Second).Infof(testlog)
|
2021-10-03 20:53:50 +08:00
|
|
|
assert.False(t, strings.Contains(buf.String(), traceKey))
|
|
|
|
assert.False(t, strings.Contains(buf.String(), spanKey))
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|