logging

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 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 normalizes (trims and lower-cases), applies defaults (info, text), reads the environment overrides named by the configuration's Env value, 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. NewEnv composes the standard names from a prefix; an empty name disables that one override, and a zero Env means no environment overrides at all.

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

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