logging

package
v0.2.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

README

pkg/core/logging

Thin wrapper around log/slog that the controller's entry points use to build the root logger. Emits logfmt by default. No JSON-output switch — everything reads through kubectl logs or a log aggregator that handles logfmt fine.

API

import "gitlab.com/haproxy-haptic/haptic/pkg/core/logging"

// Dynamic: level stored in a package-global slog.LevelVar that SetLevel()
// updates at runtime. The controller uses this so the CRD's
// logging.level field can change verbosity without a pod restart.
logger := logging.NewDynamicLogger("INFO")

logging.SetLevel("DEBUG")      // updates the dynamic level
current := logging.GetLevel()  // "ERROR" | "WARN" | "INFO" | "DEBUG" | "TRACE"

Level parsing is case-insensitive. WARNING is accepted as an alias for WARN. Empty or unknown strings fall back to INFO.

TRACE is not a native slog level; this package maps it to slog.Level(-8) (below DEBUG), matching what the rest of the controller passes through to filter-debug / per-resource-iteration logging. The value is exported as logging.LevelTrace so callers can emit trace lines without spelling out the magic number:

logger.Log(ctx, logging.LevelTrace, "store list called", "resource_type", t)

logging.LevelNameTrace ("TRACE") is exported alongside it for symmetry with slog.LevelInfo etc.

Runtime Rewiring

The controller wires up a NewDynamicLogger at startup using the LOG_LEVEL environment variable, then calls SetLevel from the configloader when a new HAProxyTemplateConfig CRD arrives with a non-empty spec.logging.level. The CRD value wins over LOG_LEVEL once it has been successfully loaded. See docs/controller/docs/troubleshooting.md#enable-debug-logging for the user-facing view.

Log-Line Style

This package doesn't enforce anything beyond "logfmt via slog". The conventions the codebase follows (messages start with a capital letter, structured key=value attributes, component-tagged child loggers) live in pkg/core/CLAUDE.md.

See Also

  • pkg/core/CLAUDE.md — log-line style conventions and examples
  • docs/controller/docs/troubleshooting.md — operator view (LOG_LEVEL env var + CRD logging.level field)
  • log/slog — upstream standard library

License

Apache-2.0 — see root LICENSE.

Documentation

Overview

Package logging provides structured logging setup using Go's standard library log/slog package.

The logging package configures slog with logfmt format (human-readable key=value pairs) and maps string log levels (ERROR, WARNING, INFO, DEBUG, TRACE) to slog levels.

Dynamic log level updates are supported via SetLevel(), which updates the global log level at runtime without requiring logger recreation.

Index

Constants

View Source
const (
	LevelNameError = "ERROR"
	LevelNameWarn  = "WARN"
	LevelNameInfo  = "INFO"
	LevelNameDebug = "DEBUG"
	LevelNameTrace = "TRACE"
)

Log level string constants.

View Source
const LevelTrace = slog.Level(-8)

LevelTrace is a log level below Debug, used for very verbose diagnostic output. Following slog convention of 4-level gaps: TRACE=-8, DEBUG=-4, INFO=0, WARN=4, ERROR=8.

Variables

This section is empty.

Functions

func CountsGroup

func CountsGroup(key string, counts map[string]int) slog.Attr

CountsGroup turns a map of named counts into a single slog group attribute so each counter is emitted as its own logfmt key (e.g. resource_counts.ingresses=9) instead of a non-logfmt Go map literal (resource_counts="map[ingresses:9 ...]"). Keys are sorted for stable output.

func GetLevel

func GetLevel() string

GetLevel returns the current global log level as a string.

func NewDynamicLogger

func NewDynamicLogger(level string) *slog.Logger

NewDynamicLogger creates a logger with a dynamically adjustable level. The level can be changed at runtime via SetLevel(). Supported levels (case-insensitive): ERROR, WARNING, INFO, DEBUG, TRACE. Invalid levels default to INFO. Uses logfmt format for output.

func ParseLogLevel

func ParseLogLevel(level string) slog.Level

ParseLogLevel converts string log level to slog.Level. Returns slog.LevelInfo for invalid or empty levels (safe default). Supported levels (case-insensitive): ERROR, WARNING/WARN, INFO, DEBUG, TRACE.

func SetLevel

func SetLevel(level string)

SetLevel updates the global log level at runtime. This affects all loggers created with NewDynamicLogger. Supported levels (case-insensitive): ERROR, WARNING, INFO, DEBUG, TRACE. Invalid levels are silently ignored (level remains unchanged).

Types

This section is empty.

Jump to

Keyboard shortcuts

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