logging

package
v0.1.0-alpha.9 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

README

pkg/core/logging

Structured logging setup using Go's standard log/slog package.

Overview

Provides utilities for initializing and configuring structured logging throughout the controller.

Installation

import "haptic/pkg/core/logging"

Quick Start

import (
    "log/slog"
    "haptic/pkg/core/logging"
)

// Initialize logger
logger := logging.New(logging.Config{
    Level:  slog.LevelInfo,
    Format: logging.FormatJSON,
})

slog.SetDefault(logger)

// Use throughout application
slog.Info("controller started",
    "namespace", namespace,
    "watched_resources", len(watchedResources))

Log Levels

  • LevelDebug: Verbose diagnostic information
  • LevelInfo: General operational information
  • LevelWarn: Non-critical issues
  • LevelError: Error conditions

Structured Logging

// Good - structured attributes
slog.Info("reconciliation completed",
    "duration_ms", duration.Milliseconds(),
    "resources_processed", count)

// Avoid - unstructured string formatting
slog.Info(fmt.Sprintf("Reconciliation completed in %dms", duration.Milliseconds()))

Context Logger

// Create logger with context
logger := slog.Default().With(
    "component", "reconciler",
    "namespace", namespace,
)

logger.Info("starting")  // Includes component=reconciler

License

See main repository for license information.

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 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 NewLogger

func NewLogger(level string) *slog.Logger

NewLogger creates a new structured logger with the specified log level. Supported levels (case-insensitive): ERROR, WARNING, INFO, DEBUG, TRACE. Invalid levels default to INFO. Uses logfmt format for output.

Note: This creates a logger with a static level. For dynamic level updates, use NewDynamicLogger instead.

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