You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
267 lines
6.5 KiB
Go
267 lines
6.5 KiB
Go
4 years ago
|
package logx
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
3 years ago
|
"encoding/json"
|
||
2 years ago
|
"fmt"
|
||
3 years ago
|
"io"
|
||
4 years ago
|
"strings"
|
||
4 years ago
|
"sync/atomic"
|
||
4 years ago
|
"testing"
|
||
4 years ago
|
"time"
|
||
4 years ago
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
3 years ago
|
"go.opentelemetry.io/otel"
|
||
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||
4 years ago
|
)
|
||
|
|
||
|
func TestTraceLog(t *testing.T) {
|
||
3 years ago
|
SetLevel(InfoLevel)
|
||
|
w := new(mockWriter)
|
||
|
old := writer.Swap(w)
|
||
3 years ago
|
writer.lock.RLock()
|
||
|
defer func() {
|
||
|
writer.lock.RUnlock()
|
||
|
writer.Store(old)
|
||
|
}()
|
||
3 years ago
|
|
||
3 years ago
|
otp := otel.GetTracerProvider()
|
||
|
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||
|
otel.SetTracerProvider(tp)
|
||
|
defer otel.SetTracerProvider(otp)
|
||
|
|
||
2 years ago
|
ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
|
||
3 years ago
|
defer span.End()
|
||
|
|
||
3 years ago
|
WithContext(ctx).Info(testlog)
|
||
|
validate(t, w.String(), true, true)
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
func TestTraceError(t *testing.T) {
|
||
3 years ago
|
w := new(mockWriter)
|
||
|
old := writer.Swap(w)
|
||
3 years ago
|
writer.lock.RLock()
|
||
|
defer func() {
|
||
|
writer.lock.RUnlock()
|
||
|
writer.Store(old)
|
||
|
}()
|
||
3 years ago
|
|
||
3 years ago
|
otp := otel.GetTracerProvider()
|
||
|
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||
|
otel.SetTracerProvider(tp)
|
||
|
defer otel.SetTracerProvider(otp)
|
||
|
|
||
2 years ago
|
ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
|
||
3 years ago
|
defer span.End()
|
||
|
|
||
3 years ago
|
var nilCtx context.Context
|
||
3 years ago
|
l := WithContext(context.Background())
|
||
3 years ago
|
l = l.WithContext(nilCtx)
|
||
|
l = l.WithContext(ctx)
|
||
3 years ago
|
SetLevel(ErrorLevel)
|
||
4 years ago
|
l.WithDuration(time.Second).Error(testlog)
|
||
3 years ago
|
validate(t, w.String(), true, true)
|
||
|
w.Reset()
|
||
4 years ago
|
l.WithDuration(time.Second).Errorf(testlog)
|
||
3 years ago
|
validate(t, w.String(), true, true)
|
||
|
w.Reset()
|
||
3 years ago
|
l.WithDuration(time.Second).Errorv(testlog)
|
||
3 years ago
|
validate(t, w.String(), true, true)
|
||
|
w.Reset()
|
||
2 years ago
|
l.WithDuration(time.Second).Errorw(testlog, Field("basket", "ball"))
|
||
3 years ago
|
validate(t, w.String(), true, true)
|
||
2 years ago
|
assert.True(t, strings.Contains(w.String(), "basket"), w.String())
|
||
|
assert.True(t, strings.Contains(w.String(), "ball"), w.String())
|
||
4 years ago
|
}
|
||
|
|
||
|
func TestTraceInfo(t *testing.T) {
|
||
3 years ago
|
w := new(mockWriter)
|
||
|
old := writer.Swap(w)
|
||
3 years ago
|
writer.lock.RLock()
|
||
|
defer func() {
|
||
|
writer.lock.RUnlock()
|
||
|
writer.Store(old)
|
||
|
}()
|
||
3 years ago
|
|
||
3 years ago
|
otp := otel.GetTracerProvider()
|
||
|
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||
|
otel.SetTracerProvider(tp)
|
||
|
defer otel.SetTracerProvider(otp)
|
||
|
|
||
2 years ago
|
ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
|
||
3 years ago
|
defer span.End()
|
||
|
|
||
4 years ago
|
SetLevel(InfoLevel)
|
||
3 years ago
|
l := WithContext(ctx)
|
||
4 years ago
|
l.WithDuration(time.Second).Info(testlog)
|
||
3 years ago
|
validate(t, w.String(), true, true)
|
||
|
w.Reset()
|
||
4 years ago
|
l.WithDuration(time.Second).Infof(testlog)
|
||
3 years ago
|
validate(t, w.String(), true, true)
|
||
|
w.Reset()
|
||
3 years ago
|
l.WithDuration(time.Second).Infov(testlog)
|
||
3 years ago
|
validate(t, w.String(), true, true)
|
||
|
w.Reset()
|
||
2 years ago
|
l.WithDuration(time.Second).Infow(testlog, Field("basket", "ball"))
|
||
3 years ago
|
validate(t, w.String(), true, true)
|
||
2 years ago
|
assert.True(t, strings.Contains(w.String(), "basket"), w.String())
|
||
|
assert.True(t, strings.Contains(w.String(), "ball"), w.String())
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
func TestTraceInfoConsole(t *testing.T) {
|
||
3 years ago
|
old := atomic.SwapUint32(&encoding, jsonEncodingType)
|
||
|
defer atomic.StoreUint32(&encoding, old)
|
||
3 years ago
|
|
||
3 years ago
|
w := new(mockWriter)
|
||
|
o := writer.Swap(w)
|
||
3 years ago
|
writer.lock.RLock()
|
||
|
defer func() {
|
||
|
writer.lock.RUnlock()
|
||
|
writer.Store(o)
|
||
|
}()
|
||
3 years ago
|
|
||
3 years ago
|
otp := otel.GetTracerProvider()
|
||
|
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||
|
otel.SetTracerProvider(tp)
|
||
|
defer otel.SetTracerProvider(otp)
|
||
|
|
||
2 years ago
|
ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
|
||
3 years ago
|
defer span.End()
|
||
|
|
||
3 years ago
|
l := WithContext(ctx)
|
||
3 years ago
|
SetLevel(InfoLevel)
|
||
|
l.WithDuration(time.Second).Info(testlog)
|
||
3 years ago
|
validate(t, w.String(), true, true)
|
||
|
w.Reset()
|
||
3 years ago
|
l.WithDuration(time.Second).Infof(testlog)
|
||
3 years ago
|
validate(t, w.String(), true, true)
|
||
|
w.Reset()
|
||
3 years ago
|
l.WithDuration(time.Second).Infov(testlog)
|
||
3 years ago
|
validate(t, w.String(), true, true)
|
||
3 years ago
|
}
|
||
|
|
||
4 years ago
|
func TestTraceSlow(t *testing.T) {
|
||
3 years ago
|
w := new(mockWriter)
|
||
|
old := writer.Swap(w)
|
||
3 years ago
|
writer.lock.RLock()
|
||
|
defer func() {
|
||
|
writer.lock.RUnlock()
|
||
|
writer.Store(old)
|
||
|
}()
|
||
3 years ago
|
|
||
3 years ago
|
otp := otel.GetTracerProvider()
|
||
|
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||
|
otel.SetTracerProvider(tp)
|
||
|
defer otel.SetTracerProvider(otp)
|
||
|
|
||
2 years ago
|
ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
|
||
3 years ago
|
defer span.End()
|
||
|
|
||
3 years ago
|
l := WithContext(ctx)
|
||
4 years ago
|
SetLevel(InfoLevel)
|
||
|
l.WithDuration(time.Second).Slow(testlog)
|
||
3 years ago
|
assert.True(t, strings.Contains(w.String(), traceKey))
|
||
|
assert.True(t, strings.Contains(w.String(), spanKey))
|
||
|
w.Reset()
|
||
4 years ago
|
l.WithDuration(time.Second).Slowf(testlog)
|
||
3 years ago
|
validate(t, w.String(), true, true)
|
||
|
w.Reset()
|
||
3 years ago
|
l.WithDuration(time.Second).Slowv(testlog)
|
||
3 years ago
|
validate(t, w.String(), true, true)
|
||
|
w.Reset()
|
||
2 years ago
|
l.WithDuration(time.Second).Sloww(testlog, Field("basket", "ball"))
|
||
3 years ago
|
validate(t, w.String(), true, true)
|
||
2 years ago
|
assert.True(t, strings.Contains(w.String(), "basket"), w.String())
|
||
|
assert.True(t, strings.Contains(w.String(), "ball"), w.String())
|
||
4 years ago
|
}
|
||
|
|
||
|
func TestTraceWithoutContext(t *testing.T) {
|
||
3 years ago
|
w := new(mockWriter)
|
||
|
old := writer.Swap(w)
|
||
3 years ago
|
writer.lock.RLock()
|
||
|
defer func() {
|
||
|
writer.lock.RUnlock()
|
||
|
writer.Store(old)
|
||
|
}()
|
||
3 years ago
|
|
||
|
l := WithContext(context.Background())
|
||
4 years ago
|
SetLevel(InfoLevel)
|
||
4 years ago
|
l.WithDuration(time.Second).Info(testlog)
|
||
3 years ago
|
validate(t, w.String(), false, false)
|
||
|
w.Reset()
|
||
4 years ago
|
l.WithDuration(time.Second).Infof(testlog)
|
||
3 years ago
|
validate(t, w.String(), false, false)
|
||
|
}
|
||
|
|
||
2 years ago
|
func TestLogWithFields(t *testing.T) {
|
||
|
w := new(mockWriter)
|
||
|
old := writer.Swap(w)
|
||
|
writer.lock.RLock()
|
||
|
defer func() {
|
||
|
writer.lock.RUnlock()
|
||
|
writer.Store(old)
|
||
|
}()
|
||
|
|
||
2 years ago
|
ctx := ContextWithFields(context.Background(), Field("foo", "bar"))
|
||
2 years ago
|
l := WithContext(ctx)
|
||
|
SetLevel(InfoLevel)
|
||
|
l.Info(testlog)
|
||
|
|
||
|
var val mockValue
|
||
|
assert.Nil(t, json.Unmarshal([]byte(w.String()), &val))
|
||
|
assert.Equal(t, "bar", val.Foo)
|
||
|
}
|
||
|
|
||
2 years ago
|
func TestLogWithCallerSkip(t *testing.T) {
|
||
|
w := new(mockWriter)
|
||
|
old := writer.Swap(w)
|
||
|
writer.lock.RLock()
|
||
|
defer func() {
|
||
|
writer.lock.RUnlock()
|
||
|
writer.Store(old)
|
||
|
}()
|
||
|
|
||
|
l := WithCallerSkip(1).WithCallerSkip(0)
|
||
|
p := func(v string) {
|
||
|
l.Info(v)
|
||
|
}
|
||
|
|
||
|
file, line := getFileLine()
|
||
|
p(testlog)
|
||
|
assert.True(t, w.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||
|
|
||
|
w.Reset()
|
||
|
l = WithCallerSkip(0).WithCallerSkip(1)
|
||
|
file, line = getFileLine()
|
||
|
p(testlog)
|
||
|
assert.True(t, w.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||
|
}
|
||
|
|
||
3 years ago
|
func validate(t *testing.T, body string, expectedTrace, expectedSpan bool) {
|
||
|
var val mockValue
|
||
3 years ago
|
dec := json.NewDecoder(strings.NewReader(body))
|
||
|
|
||
|
for {
|
||
|
var doc mockValue
|
||
|
err := dec.Decode(&doc)
|
||
|
if err == io.EOF {
|
||
|
// all done
|
||
|
break
|
||
|
}
|
||
|
if err != nil {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
val = doc
|
||
|
}
|
||
|
|
||
3 years ago
|
assert.Equal(t, expectedTrace, len(val.Trace) > 0, body)
|
||
|
assert.Equal(t, expectedSpan, len(val.Span) > 0, body)
|
||
3 years ago
|
}
|
||
|
|
||
|
type mockValue struct {
|
||
|
Trace string `json:"trace"`
|
||
|
Span string `json:"span"`
|
||
2 years ago
|
Foo string `json:"foo"`
|
||
4 years ago
|
}
|