logging

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 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 package exports:

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 runs, in order:

  • composes its override names from the prefix it receives with NewEnv, recording them on Env
  • normalizes (trims and lower-cases)
  • applies defaults (info, text)
  • reads the overrides
  • normalizes again
  • 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.

Construction

New returns a logger writing to the caller's io.Writer, with a JSON handler for FormatJSON and a text handler otherwise. Finalize is the validation point: New returns no error, and a Config that skipped Finalize yields an info-level logger.

Record times

Both handlers write each record's time in time.UTC, whatever time.Local is, so it ends in "Z" in either format. Only the record's own time is converted: a time.Time the caller passes as an attribute is written as the caller built it.

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, with a JSON handler for FormatJSON and a text handler otherwise. A level Finalize would reject falls back to info. Either handler writes each record's time in time.UTC.

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, applies defaults and the envPrefix-named overrides, then 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 <PREFIX>_LOG_LEVEL and <PREFIX>_LOG_FORMAT with config.EnvName; an empty prefix returns the zero Env.

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

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

Jump to

Keyboard shortcuts

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