| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package logtest_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"go.opentelemetry.io/otel/log" |
| 11 |
"go.opentelemetry.io/otel/log/logtest" |
| 12 |
) |
| 13 |
|
| 14 |
func Example() { |
| 15 |
t := &testing.T{} // Provided by testing framework. |
| 16 |
|
| 17 |
// Create a recorder. |
| 18 |
rec := logtest.NewRecorder() |
| 19 |
|
| 20 |
// Emit a log record (code under test). |
| 21 |
l := rec.Logger("Example") |
| 22 |
r := log.Record{} |
| 23 |
r.SetTimestamp(time.Now()) |
| 24 |
r.SetSeverity(log.SeverityInfo) |
| 25 |
r.SetBody(log.StringValue("Hello there")) |
| 26 |
r.AddAttributes(log.String("foo", "bar")) |
| 27 |
r.AddAttributes(log.Int("n", 1)) |
| 28 |
l.Emit(t.Context(), r) |
| 29 |
|
| 30 |
// Verify that the expected and actual log records match. |
| 31 |
want := logtest.Recording{ |
| 32 |
logtest.Scope{Name: "Example"}: []logtest.Record{ |
| 33 |
{ |
| 34 |
Severity: log.SeverityInfo, |
| 35 |
Body: log.StringValue("Hello there"), |
| 36 |
Attributes: []log.KeyValue{ |
| 37 |
log.Int("n", 1), |
| 38 |
log.String("foo", "bar"), |
| 39 |
}, |
| 40 |
}, |
| 41 |
}, |
| 42 |
} |
| 43 |
got := rec.Result() |
| 44 |
logtest.AssertEqual(t, want, got, |
| 45 |
logtest.Transform(func(r logtest.Record) logtest.Record { |
| 46 |
r = r.Clone() |
| 47 |
r.Context = nil // Ignore context. |
| 48 |
r.Timestamp = time.Time{} // Ignore timestamp. |
| 49 |
return r |
| 50 |
}), |
| 51 |
) |
| 52 |
// Output: |
| 53 |
// |
| 54 |
} |