logging

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 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.

The standard library already owns the level vocabulary: slog.Level parses "debug", "INFO", and offsets such as "warn+2" from text, case-insensitively. This package adds only what slog has no opinion about — the configuration seam and the handler choice — so it defines no level enum of its own.

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 applies defaults (info, text), then the environment overrides named by the Env value the configuration carries, then lower-cases both fields, then validates. Normalizing after the environment read is what lets a value arrive from a file or a shell in any casing and validate identically. NewEnv composes the standard names from a prefix; an empty name disables that one override, so a zero Env means no environment overrides at all.

Level is a string rather than a slog.Level because slog.LevelInfo is the zero value of that type, and a configuration that merges layers has to tell "set to info" from "unset". An empty Level is unset; Level.Slog rejects it, and Finalize has already 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 writer is a parameter rather than a configuration field because a configuration carries values and is discarded; a caller that needs to capture output supplies its own writer.

New returns no error, because Finalize is where a configuration is validated. A caller that builds a Config literal and skips Finalize gets info rather than a failure, so a misconfiguration costs the process a level, not its logs.

Scope

This package constructs a logger; it does not carry one. Subsystems take a *slog.Logger as an ordinary dependency, and the HTTP request logger lives in the web package, so a consumer that never serves HTTP does not compile net/http to get a logger.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

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

Types

type Config

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

func (*Config) Finalize

func (c *Config) Finalize() error

func (*Config) Merge

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

type Env

type Env struct {
	Level  string
	Format string
}

func NewEnv

func NewEnv(prefix string) Env

type Format

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

func (Format) String

func (f Format) String() string

func (Format) Valid

func (f Format) Valid() bool

type Level

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

func (Level) Slog

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

func (Level) String

func (l Level) String() string

Jump to

Keyboard shortcuts

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