logging

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

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 this when you need to wrap the handler with middleware (e.g., trace injection) before creating the final logger.

Defaults:

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 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