logging

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Component

func Component(name string) *slog.Logger

Component returns a logger with a "component" attribute set to the given name. Use this to create component-specific loggers for structured logging.

func ComponentWithContext

func ComponentWithContext(ctx context.Context, component string) *slog.Logger

ComponentWithContext returns a logger with both component and trace_id attributes. This is the recommended way to create component-specific loggers.

Usage:

log := logging.ComponentWithContext(ctx, "storage")
log.Info("bucket created", "bucket", "my-bucket")

The log will include both component="storage" and trace_id from context.

func Default

func Default() *slog.Logger

Default returns the default logger. If Setup has not been called, returns slog's default logger.

func DiscardLogger

func DiscardLogger() *log.Logger

DiscardLogger returns a standard library logger that discards all output. Use this for noisy third-party libraries that should be silenced.

func FromContext

func FromContext(ctx context.Context) *slog.Logger

FromContext returns a logger with the trace_id automatically included from context. This is the recommended way to get a logger - it ensures trace_id is never forgotten.

Usage:

log := logging.FromContext(ctx)
log.Info("operation completed", "details", "some info")

The trace_id will be automatically included in the log output.

func RedactHeaders added in v0.5.2

func RedactHeaders(h http.Header) http.Header

RedactHeaders returns a copy of headers safe for logging, with sensitive values replaced by RedactString. Use this instead of logging http.Header directly, since it commonly carries Authorization/cookie credentials.

func RedactString added in v0.5.2

func RedactString(s string) string

RedactString masks a secret-bearing string for logging, keeping only enough of it (a short prefix) to correlate log lines during debugging without exposing the value itself.

func Setup

func Setup(cfg Config)

Setup initializes the global logger with the given configuration. This should be called once at application startup.

func ShouldLogSuppressible

func ShouldLogSuppressible() bool

ShouldLogSuppressible returns true if suppressible messages should be logged. Suppressible messages are routine/noisy messages that can be hidden in quiet mode. Returns false only when verbosity is "quiet".

func ShouldLogVerbose

func ShouldLogVerbose() bool

ShouldLogVerbose returns true if verbose messages should be logged. Verbose messages are extra diagnostic output only shown in verbose mode. Returns true only when verbosity is "verbose".

func StdLogger

func StdLogger(component string) *log.Logger

StdLogger returns a standard library *log.Logger for third-party libraries that don't support slog. The logger respects the global verbosity setting: - In quiet mode: discards all output - Otherwise: writes to stderr with a component prefix

func WithContext

func WithContext(logger *slog.Logger, ctx context.Context) *slog.Logger

WithContext adds trace_id to an existing logger. Use this when you already have a logger and need to add trace context.

Usage:

log := logging.Component("server").WithContext(ctx)
log.Info("request processed")

Types

type Config

type Config struct {
	Level     string    // debug, info, warn, error
	Format    string    // text, json
	Verbosity string    // quiet, normal, verbose
	Output    io.Writer // defaults to os.Stderr
}

Config holds logger configuration

type Verbosity

type Verbosity int

Verbosity represents the chattiness level of log output. This is separate from log levels (debug, info, warn, error) - verbosity controls how much detail components output at any given level.

const (
	// VerbosityQuiet suppresses suppressible messages (routine noise)
	VerbosityQuiet Verbosity = -1
	// VerbosityNormal is the default verbosity level
	VerbosityNormal Verbosity = 0
	// VerbosityVerbose enables extra diagnostic output
	VerbosityVerbose Verbosity = 1
)

func GetVerbosity

func GetVerbosity() Verbosity

GetVerbosity returns the global verbosity level.

func ParseVerbosity

func ParseVerbosity(s string) Verbosity

ParseVerbosity converts a string to a Verbosity level. Accepts "quiet", "normal", or "verbose". Defaults to VerbosityNormal.

func (Verbosity) String

func (v Verbosity) String() string

String returns the string representation of the verbosity level.

Jump to

Keyboard shortcuts

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