logging

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package logging constructs the *slog.Logger a process writes through, from a configuration that takes part in the layered load.

Configuration

Config holds a Level and a Format and implements the config package's Merge and Finalize contract, so it loads as part of an application's configuration rather than on its own. Finalize composes its environment override names from the prefix it receives (via NewEnv, recorded on Env for introspection), normalizes (trims and lower-cases), applies defaults (info, text), reads the overrides, normalizes again, and validates. A value from a file or a shell therefore validates identically in any casing, and a blank or whitespace-only value takes the default. An empty prefix composes no names, disabling the overrides.

Level is a string. slog.Level parses it — "debug", "INFO", and offsets such as "warn+2", case-insensitively — through Level.Slog. An empty Level is unset, distinct from "set to info"; Finalize has replaced it with a default by the time validation runs.

Construction

New returns a logger writing to the caller's io.Writer, with slog.NewJSONHandler selected by FormatJSON and slog.NewTextHandler otherwise. The caller supplies the writer; a test passes a buffer.

New returns no error; Finalize is the validation point. A Config literal that skipped Finalize yields an info-level logger.

Scope

Subsystems take a *slog.Logger as an ordinary dependency. The HTTP request logger is in the web package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(w io.Writer, cfg Config) *slog.Logger

New returns a logger writing to w: a JSON handler for FormatJSON, a text handler otherwise. New returns no error — Finalize is the validation point — and an unparseable level falls back to info.

Types

type Config

type Config struct {
	Level  Level  `json:"level"`
	Format Format `json:"format"`
	Env    Env    `json:"-"`
}

Config selects the level and format the process logger is built with. Env records the environment-variable names Finalize composed and read; it is excluded from JSON.

func (*Config) Finalize

func (c *Config) Finalize(envPrefix string) error

Finalize normalizes the values (trimming and lower-casing), applies defaults (info, text), applies the environment overrides named by Env, and validates.

func (*Config) Merge

func (c *Config) Merge(src *Config)

Merge overlays src's set fields onto the receiver.

type Env

type Env struct {
	Level  string
	Format string
}

Env names the environment variables Config.Finalize reads. An empty name disables that one override; the zero value disables both.

func NewEnv

func NewEnv(prefix string) Env

NewEnv composes the standard override names from a prefix: LOG_LEVEL and LOG_FORMAT under whatever prefix config.EnvName produces.

type Format

type Format string

Format selects the slog handler New constructs.

const (
	FormatText Format = "text"
	FormatJSON Format = "json"
)

The supported output formats.

func (Format) String

func (f Format) String() string

String returns the format's string value.

func (Format) Valid

func (f Format) Valid() bool

Valid reports whether the format is one New recognizes.

type Level

type Level string

Level is the minimum level the logger emits, in the vocabulary slog.Level parses: "debug", "INFO", and offsets such as "warn+2", case-insensitively. An empty Level is unset, distinct from "set to info".

const (
	LevelDebug Level = "debug"
	LevelInfo  Level = "info"
	LevelWarn  Level = "warn"
	LevelError Level = "error"
)

The standard levels; any string slog.Level parses is valid.

func (Level) Slog

func (l Level) Slog() (slog.Level, error)

Slog parses the level into a slog.Level.

func (Level) String

func (l Level) String() string

String returns the level's string value.

Jump to

Keyboard shortcuts

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