logging

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package logging provides structured logging helpers built on top of log/slog. It configures the global logger with JSON or text output and log levels (adjustable at runtime via SetLevel), and provides a ContextLogger for per-request IDs. Log output goes to the configured writer (default stderr); pipe it to your process supervisor or a log shipper for rotation/retention.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromContext

func FromContext(ctx context.Context) *slog.Logger

FromContext retrieves the logger from context, falling back to slog.Default().

func New

func New(cfg Config) *slog.Logger

New creates a slog.Logger from config and sets it as the global logger. The level can be changed later at runtime with SetLevel.

func RequestID

func RequestID(ctx context.Context) string

RequestID retrieves the request ID from context.

func SetLevel

func SetLevel(level slog.Level)

SetLevel changes the minimum log level at runtime for all loggers created by New.

func SetLevelString

func SetLevelString(s string)

SetLevelString changes the minimum log level at runtime from a string ("debug", "info", "warn", "error"). Unknown values default to info and log a warning.

func WithRequestID

func WithRequestID(ctx context.Context, log *slog.Logger, id string) (context.Context, *slog.Logger)

WithRequestID returns a context and logger enriched with the request ID.

Types

type Config

type Config struct {
	// Level is the minimum log level: "debug", "info", "warn", "error" (default: "info").
	Level string
	// Format is "json" or "text". When empty it defaults to "text" in dev
	// environments (APP_ENV of "local", "dev", "development" or "debug") and
	// "json" otherwise.
	Format string
	// Output is the writer to send logs to (default: os.Stderr).
	Output io.Writer
	// AddSource includes caller file:line in each log record.
	AddSource bool
}

Config configures the logger.

Jump to

Keyboard shortcuts

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