logging

package
v0.0.51 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 5 Imported by: 6

Documentation

Overview

Package logging provides a pre-configured log/slog.Logger factory with consistent defaults for the ToolHive ecosystem.

All ToolHive projects share the same timestamp format, output destination, and handler configuration. This package encapsulates those choices so that each project does not need to replicate them.

Defaults

Basic Usage

Create a logger with default settings:

logger := logging.New()
logger.Info("server started", "port", 8080)

Configuration

Use functional options to customize the logger:

logger := logging.New(
	logging.WithFormat(logging.FormatText),
	logging.WithLevel(slog.LevelDebug),
)

Dynamic Level Changes

Pass a log/slog.LevelVar to change the level at runtime:

var lvl slog.LevelVar
logger := logging.New(logging.WithLevel(&lvl))
lvl.Set(slog.LevelDebug) // takes effect immediately

Testing

Inject a buffer to capture log output in tests:

var buf bytes.Buffer
logger := logging.New(logging.WithOutput(&buf))
logger.Info("test message")
// inspect buf.String()

Handler Access

Use NewHandler when you need to wrap the handler with middleware:

base := logging.NewHandler(logging.WithLevel(slog.LevelDebug))
wrapped := &myMiddleware{Handler: base}
logger := slog.New(wrapped)

Stability

This package is Alpha stability. The API may change without notice. See the toolhive-core README for stability level definitions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) *slog.Logger

New creates a pre-configured *log/slog.Logger with consistent defaults used across the ToolHive ecosystem.

Defaults:

func NewHandler added in v0.0.5

func NewHandler(opts ...Option) slog.Handler

NewHandler creates a pre-configured log/slog.Handler with consistent defaults used across the ToolHive ecosystem. Use WithHandlerMiddleware to wrap the handler with cross-cutting concerns (e.g., trace injection) before the final logger is created — or, for a one-off need, call NewHandler and wrap its return value directly instead of creating the final logger with New.

Defaults:

func ParseLevel added in v0.0.33

func ParseLevel(level string) (slog.Level, error)

ParseLevel parses a level name ("debug", "info", "warn", or "error") into a log/slog.Level. An empty string resolves to log/slog.LevelInfo, matching New's default.

Types

type Format

type Format int

Format represents the log output format.

const (
	// FormatJSON produces JSON-formatted log output using [log/slog.JSONHandler].
	// This is the default format, suitable for production environments.
	FormatJSON Format = iota

	// FormatText produces human-readable text output using [log/slog.TextHandler].
	// This is suitable for local development.
	FormatText
)

type Option

type Option func(*config)

Option configures New and NewHandler.

func WithFormat

func WithFormat(f Format) Option

WithFormat sets the output format (JSON or Text). The default is FormatJSON.

func WithHandlerMiddleware added in v0.0.51

func WithHandlerMiddleware(fn func(slog.Handler) slog.Handler) Option

WithHandlerMiddleware wraps the handler NewHandler builds with fn before returning it, so a caller can inject a cross-cutting concern — trace-context stamping, redaction, sampling, or anything else expressible as func(slog.Handler) slog.Handler — without this package taking a dependency on it. Applying more than one middleware is equivalent to manually chaining fn calls in the given order: the last WithHandlerMiddleware call wraps every earlier one, so it is the outermost handler and sees (and can filter or annotate) each record before the others do.

handler := logging.NewHandler(
	logging.WithFormat(logging.FormatText),
	logging.WithHandlerMiddleware(myTraceContextHandler),
)

func WithLevel

func WithLevel(l slog.Leveler) Option

WithLevel sets the minimum log level. The default is log/slog.LevelInfo.

Accepts any log/slog.Leveler, including *log/slog.LevelVar for dynamic level changes:

var lvl slog.LevelVar
lvl.Set(slog.LevelDebug)
logger := logging.New(logging.WithLevel(&lvl))

func WithOutput

func WithOutput(w io.Writer) Option

WithOutput sets the destination writer for log output. The default is os.Stderr.

Jump to

Keyboard shortcuts

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