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 ¶
- Format: JSON (FormatJSON) via log/slog.JSONHandler
- Level: INFO (log/slog.LevelInfo)
- Output: os.Stderr
- Timestamps: time.RFC3339
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 ¶
New creates a pre-configured *log/slog.Logger with consistent defaults used across the ToolHive ecosystem.
Defaults:
- Format: JSON (FormatJSON)
- Level: INFO (log/slog.LevelInfo)
- Output: os.Stderr
- Timestamps: time.RFC3339
func NewHandler ¶ added in v0.0.5
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:
- Format: JSON (FormatJSON)
- Level: INFO (log/slog.LevelInfo)
- Output: os.Stderr
- Timestamps: time.RFC3339
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 ¶
WithFormat sets the output format (JSON or Text). The default is FormatJSON.
func WithLevel ¶
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))