slogmem

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package slogmem provides a slog.Handler that captures log records in memory.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler captures records produced by a call to Handle in-memory so that they can be accessed via LoggedRecords later for inspection.

func NewHandler

func NewHandler(leveler slog.Leveler) *Handler

NewHandler creates a new in-memory Handler that captures log records which have a level greater than or equal to the current level of the given leveler.

func (*Handler) Enabled

func (h *Handler) Enabled(_ context.Context, level slog.Level) bool

Enabled returns whether the Handler is currently enabled for the given slog.Level. Levels greater than or equal to that of the Handler's slog.Leveler's current Level are considered enabled.

func (*Handler) Handle

func (h *Handler) Handle(_ context.Context, record slog.Record) error

Handle stores the content of the slog.Record into the Handler's in-memory LoggedRecords store.

Handle will only be called when [Enabled] returns true.

func (*Handler) Records

func (h *Handler) Records() *LoggedRecords

Records returns the LoggedRecords that were recorded by this Handler.

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new Handler whose attributes consist of both the existing handler's attributes and those given.

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup returns a new Handler that will store all future attributes under a group with the given name.

type LoggedRecord

type LoggedRecord struct {
	// Time is the time the log was written.
	Time time.Time
	// Level is the [slog.Level] that the log was written as.
	Level slog.Level
	// Message is the message that was passed by the caller for the given log entry.
	Message string
	// Attrs is a slice of [slog.Attr] records that represent the additional
	// attributes that were added to the log entry by the caller as context.
	Attrs []slog.Attr
}

LoggedRecord encapsulates the information that was recorded in a single log entry by the Handler.

type LoggedRecords

type LoggedRecords struct {
	// contains filtered or unexported fields
}

LoggedRecords is a slice of LoggedRecord entries that were captured by a Handler. Adding to LoggedRecords is safe to do concurrently.

func NewLoggedRecords

func NewLoggedRecords(records []LoggedRecord) *LoggedRecords

NewLoggedRecords encapsulates the given list of LoggedRecord entries within a LoggedRecords struct to represent the list of logged records in a way that is easy to lookup when asserting logs in tests or similar.

func (*LoggedRecords) AsSliceOfNestedKeyValuePairs

func (lr *LoggedRecords) AsSliceOfNestedKeyValuePairs() []map[string]any

AsSliceOfNestedKeyValuePairs flattens the LoggedRecords so that they can be accessed as a series of key value pair objects representing each recorded log.

This method would be used when formatting the recorded log records as JSON for example.

func (*LoggedRecords) Contains

func (lr *LoggedRecords) Contains(query RecordQuery) (bool, string)

Contains can be used to check if a LoggedRecords contains a LoggedRecord that matches the details in the given RecordQuery.

Contains returns true for the first record that fully matches the given RecordQuery. If there are no records matching the given query then false will be returned.

There is an additional argument returned as a convenience helper which provides a diff of the passed RecordQuery and the LoggedRecords when there is no match or a partial match on the message. When Contains returns true, the diff (second return value) will be empty. When there is not a full match on a record but the message matches, a diff will be produced for each record matching that message. Otherwise, a diff over all records that were logged will be produced.

NOTE: this diff is nondeterministic, do not rely on its output. This is a convenience helper for logging information in failed tests and similar scenarios.

func (*LoggedRecords) IsEmpty added in v1.1.0

func (lr *LoggedRecords) IsEmpty() bool

IsEmpty returns true when no records have been captured.

func (*LoggedRecords) Len added in v1.2.0

func (lr *LoggedRecords) Len() int

Len returns the number of records that have been captured.

type RecordQuery

type RecordQuery struct {
	// Level is the [slog.Level] that the log was written as.
	Level slog.Level
	// Message is the message that was passed by the caller for the given log entry.
	Message string
	// Attrs is a map of dot separated keys that each indicate a path to a grouped
	// attribute and the value of that attribute. For example: if an attribute was
	// written as `slog.Group("group", slog.String("key", "value"))` then to query
	// that, we would pass `map[string]slog.Value{"group.key": slog.StringValue("value")}`.
	Attrs map[string]slog.Value
}

RecordQuery represents the relevant information required in order to query for the existence of a LoggedRecord within a set of LoggedRecords. Time is not part of the query as it is generally difficult to know when the log was written in order to query for it accurately.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳