logtest

package module
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2026 License: Apache-2.0, BSD-3-Clause Imports: 8 Imported by: 2

README

Log Test

PkgGoDev

Documentation

Overview

Package logtest is a testing helper package.

Example
package main

import (
	"testing"
	"time"

	"go.opentelemetry.io/otel/log"
	"go.opentelemetry.io/otel/log/logtest"
)

func main() {
	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))
	l.Emit(t.Context(), r)

	// 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()
	logtest.AssertEqual(
		t, want, got,
		logtest.Transform(func(r logtest.Record) logtest.Record {
			r = r.Clone()
			r.Context = nil           // Ignore context.
			r.Timestamp = time.Time{} // Ignore timestamp.
			return r
		}),
	)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertEqual

func AssertEqual[T Recording | Record](t TestingT, want, got T, opts ...AssertOption) bool

AssertEqual asserts that the two concrete data-types from the logtest package are equal.

Types

type AssertOption

type AssertOption interface {
	// contains filtered or unexported methods
}

AssertOption allows for fine grain control over how AssertEqual operates.

func Desc

func Desc(text string, args ...any) AssertOption

Desc prepends the given text to an assertion failure message. The text is formatted with the args using fmt.Sprintf.

func Transform

func Transform[A, B any](f func(A) B) AssertOption

Transform applies a transformation f function that converts values of a certain type into that of another. f must not mutate A in any way.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option configures a Recorder.

func WithEnabledFunc

func WithEnabledFunc(fn func(context.Context, log.EnabledParameters) bool) Option

WithEnabledFunc allows configuring whether the Recorder is enabled for specific log entries or not.

By default, the Recorder is enabled for every log entry.

type Record

type Record struct {
	Context           context.Context
	EventName         string
	Timestamp         time.Time
	ObservedTimestamp time.Time
	Severity          log.Severity
	SeverityText      string
	Body              log.Value
	Error             error
	Attributes        []log.KeyValue
	// contains filtered or unexported fields
}

Record represents the record alongside its context.

func (Record) Clone

func (a Record) Clone() Record

Clone returns a deep copy.

type Recorder

type Recorder struct {
	embedded.LoggerProvider
	// contains filtered or unexported fields
}

Recorder stores all received log records in-memory. Recorder implements log.LoggerProvider.

func NewRecorder

func NewRecorder(options ...Option) *Recorder

NewRecorder returns a new Recorder.

func (*Recorder) Logger

func (r *Recorder) Logger(name string, opts ...log.LoggerOption) log.Logger

Logger returns a copy of Recorder as a log.Logger with the provided scope information.

func (*Recorder) Reset

func (r *Recorder) Reset()

Reset clears the in-memory log records for all loggers.

func (*Recorder) Result

func (r *Recorder) Result() Recording

Result returns a deep copy of the current in-memory recorded log records.

type Recording

type Recording map[Scope][]Record

Recording represents the recorded log records snapshot.

type Scope

type Scope struct {
	// Name is the name of the instrumentation scope. This should be the
	// Go package name of that scope.
	Name string
	// Version is the version of the instrumentation scope.
	Version string
	// SchemaURL of the telemetry emitted by the scope.
	SchemaURL string
	// Attributes of the telemetry emitted by the scope.
	Attributes attribute.Set
}

Scope represents the instrumentation scope.

type TestingT added in v0.14.0

type TestingT interface {
	Errorf(format string, args ...any)
}

TestingT reports failure messages. *testing.T implements this interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL