2024-03-05 01:27:22 +03:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
|
|
|
"go.unistack.org/micro/v4/logger/slog"
|
|
|
|
"go.unistack.org/micro/v4/tracer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoggerWithTracer(t *testing.T) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
logger.DefaultLogger = slog.NewLogger(logger.WithOutput(buf))
|
|
|
|
|
|
|
|
if err := logger.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var span tracer.Span
|
|
|
|
tr := NewTracer()
|
|
|
|
ctx, span = tr.Start(ctx, "test1")
|
|
|
|
|
|
|
|
logger.Error(ctx, "my test error", fmt.Errorf("error"))
|
|
|
|
|
|
|
|
if !strings.Contains(buf.String(), span.TraceID()) {
|
2024-03-06 01:17:19 +03:00
|
|
|
t.Fatalf("log does not contains trace id: %s", buf.Bytes())
|
2024-03-05 01:27:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = tr.Start(ctx, "test2")
|
|
|
|
|
|
|
|
for _, s := range tr.Spans() {
|
2024-03-06 01:17:19 +03:00
|
|
|
_ = s
|
2024-03-05 01:27:22 +03:00
|
|
|
}
|
|
|
|
}
|