1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-08-10 22:31:50 +02:00

log/logtest: improve Transform usage in the example (#6938)

This commit is contained in:
Robert Pająk
2025-07-07 20:48:19 +02:00
committed by GitHub
parent 9af7382dce
commit bd5e1346ce

View File

@@ -20,20 +20,18 @@ func Example() {
// Emit a log record (code under test).
l := rec.Logger("Example")
ctx := context.Background()
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))
l.Emit(ctx, r)
l.Emit(context.Background(), r)
// Verify that the expected and actual log records match.
want := logtest.Recording{
logtest.Scope{Name: "Example"}: []logtest.Record{
{
Context: context.Background(),
Severity: log.SeverityInfo,
Body: log.StringValue("Hello there"),
Attributes: []log.KeyValue{
@@ -45,9 +43,11 @@ func Example() {
}
got := rec.Result()
logtest.AssertEqual(t, want, got,
// Ignore Timestamps.
logtest.Transform(func(time.Time) time.Time {
return time.Time{}
logtest.Transform(func(r logtest.Record) logtest.Record {
r = r.Clone()
r.Context = nil // Ignore context.
r.Timestamp = time.Time{} // Ignore timestamp.
return r
}),
)
// Output: