2025-05-20 10:54:48 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
|
|
package logtest_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/log"
|
|
|
|
|
"go.opentelemetry.io/otel/log/logtest"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Example() {
|
|
|
|
|
t := &testing.T{} // Provided by testing framework.
|
|
|
|
|
|
|
|
|
|
// Create a recorder.
|
|
|
|
|
rec := logtest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
// Emit a log record (code under test).
|
|
|
|
|
l := rec.Logger("Example")
|
|
|
|
|
r := log.Record{}
|
|
|
|
|
r.SetTimestamp(time.Now())
|
|
|
|
|
r.SetSeverity(log.SeverityInfo)
|
|
|
|
|
r.SetBody(log.StringValue("Hello there"))
|
|
|
|
|
r.AddAttributes(log.String("foo", "bar"))
|
|
|
|
|
r.AddAttributes(log.Int("n", 1))
|
2025-09-23 15:52:45 +08:00
|
|
|
l.Emit(t.Context(), r)
|
2025-05-20 10:54:48 +02:00
|
|
|
|
|
|
|
|
// Verify that the expected and actual log records match.
|
|
|
|
|
want := logtest.Recording{
|
|
|
|
|
logtest.Scope{Name: "Example"}: []logtest.Record{
|
|
|
|
|
{
|
|
|
|
|
Severity: log.SeverityInfo,
|
|
|
|
|
Body: log.StringValue("Hello there"),
|
|
|
|
|
Attributes: []log.KeyValue{
|
|
|
|
|
log.Int("n", 1),
|
|
|
|
|
log.String("foo", "bar"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
got := rec.Result()
|
2026-05-08 12:50:12 -07:00
|
|
|
logtest.AssertEqual(
|
|
|
|
|
t, want, got,
|
2025-07-07 20:48:19 +02:00
|
|
|
logtest.Transform(func(r logtest.Record) logtest.Record {
|
|
|
|
|
r = r.Clone()
|
|
|
|
|
r.Context = nil // Ignore context.
|
|
|
|
|
r.Timestamp = time.Time{} // Ignore timestamp.
|
|
|
|
|
return r
|
2025-05-20 10:54:48 +02:00
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
// Output:
|
|
|
|
|
//
|
|
|
|
|
}
|