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 ¶
- func FromContext(ctx context.Context) *slog.Logger
- func New(cfg Config) *slog.Logger
- func RequestID(ctx context.Context) string
- func SetLevel(level slog.Level)
- func SetLevelString(s string)
- func WithRequestID(ctx context.Context, log *slog.Logger, id string) (context.Context, *slog.Logger)
- type Config
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromContext ¶
FromContext retrieves the logger from context, falling back to slog.Default().
func New ¶
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 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.
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.