slogstub

package module
v0.0.0-...-1c4a867 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: GPL-3.0 Imports: 3 Imported by: 0

README

Golang Helpers for slog Testing

GoDoc Build Status codecov

The slogstub Go package contains small helpers for testing code that uses log/slog.

For example:

import (
	"context"
	"log/slog"

	"github.com/bassosimone/slogstub"
	"github.com/stretchr/testify/assert"
)

// Create a handler that captures log records.
var captured []slog.Record
handler := &slogstub.FuncHandler{
	EnabledFunc: func(ctx context.Context, level slog.Level) bool {
		return true
	},
	HandleFunc: func(ctx context.Context, record slog.Record) error {
		captured = append(captured, record)
		return nil
	},
	WithAttrsFunc: func(attrs []slog.Attr) slog.Handler {
		return handler
	},
	WithGroupFunc: func(name string) slog.Handler {
		return handler
	},
}

// Use the handler in code under test.
logger := slog.New(handler)
logger.Info("test message", "key", "value")

// Verify the captured records.
assert.Len(t, captured, 1)
assert.Equal(t, "test message", captured[0].Message)

Installation

To add this package as a dependency to your module:

go get github.com/bassosimone/slogstub

Development

To run the tests:

go test -v .

To measure test coverage:

go test -v -cover .

License

SPDX-License-Identifier: GPL-3.0-or-later

Documentation

Overview

Package slogstub provides test stubs for the log/slog package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FuncHandler

type FuncHandler struct {
	// EnabledFunc determines if the handler is enabled for the given level.
	EnabledFunc func(ctx context.Context, level slog.Level) bool

	// HandleFunc processes a log record.
	HandleFunc func(ctx context.Context, record slog.Record) error

	// WithAttrsFunc returns a new handler with additional attributes.
	WithAttrsFunc func(attrs []slog.Attr) slog.Handler

	// WithGroupFunc returns a new handler with a group name.
	WithGroupFunc func(name string) slog.Handler
}

FuncHandler implements slog.Handler using configurable functions.

This type is useful for testing code that emits structured log events, allowing tests to capture and inspect slog.Record values directly rather than parsing serialized output.

Each function field must be set before calling the corresponding method. Calling a method when its function field is nil will panic.

func (*FuncHandler) Enabled

func (h *FuncHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled implements slog.Handler.

func (*FuncHandler) Handle

func (h *FuncHandler) Handle(ctx context.Context, record slog.Record) error

Handle implements slog.Handler.

func (*FuncHandler) WithAttrs

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

WithAttrs implements slog.Handler.

func (*FuncHandler) WithGroup

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

WithGroup implements slog.Handler.

Jump to

Keyboard shortcuts

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