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