logutil

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package logutil provides a structured logging abstraction built on top of slog.

This package provides a simple, consistent logging interface for azd extensions. It wraps the standard library's slog package with convenience functions and environment-aware configuration.

Basic Usage

// Initialize logging (typically in main.go)
logutil.SetupLogger(debug, structured)

// Log messages at different levels
logutil.Debug("processing item", "id", itemID)
logutil.Info("operation completed", "duration", elapsed)
logutil.Warn("deprecated feature used", "feature", name)
logutil.Error("operation failed", "error", err)

Debug Mode

Debug logging can be enabled in two ways:

  • Pass debug=true to SetupLogger
  • Set AZD_DEBUG=true environment variable

Structured Logging

When structured=true is passed to SetupLogger, logs are output as JSON:

{"time":"2024-01-15T10:30:00Z","level":"INFO","msg":"operation completed","duration":"1.5s"}

Otherwise, logs use a human-readable text format:

time=2024-01-15T10:30:00Z level=INFO msg="operation completed" duration=1.5s

Index

Constants

View Source
const (
	// EnvDebug enables debug logging when set to "true".
	EnvDebug = "AZD_DEBUG"
)

Environment variable names for logging configuration.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, args ...any)

Debug logs a debug message with optional key-value pairs. Debug messages are only logged when debug mode is enabled.

Example:

logutil.Debug("processing request", "method", "GET", "path", "/api/users")

func Error

func Error(msg string, args ...any)

Error logs an error message with optional key-value pairs.

Example:

logutil.Error("failed to connect", "error", err, "host", dbHost)

func Info

func Info(msg string, args ...any)

Info logs an info message with optional key-value pairs.

Example:

logutil.Info("server started", "port", 8080)

func IsDebugEnabled

func IsDebugEnabled() bool

IsDebugEnabled returns true if debug logging is enabled. This checks both the programmatic setting and the AZD_DEBUG environment variable. This function is safe for concurrent use.

func Logger

func Logger() *slog.Logger

Logger returns the underlying slog.Logger for advanced usage. This function is safe for concurrent use.

func SetLevel

func SetLevel(level Level)

SetLevel sets the logging level programmatically. This function is safe for concurrent use.

func SetOutput

func SetOutput(w io.Writer)

SetOutput sets the output writer for the logger. This is useful for testing or redirecting logs. This function is safe for concurrent use.

func SetupLogger

func SetupLogger(debug, structured bool)

SetupLogger configures the global logger.

Parameters:

  • debug: When true, enables debug-level logging
  • structured: When true, outputs JSON-formatted logs; otherwise uses text format

The logger writes to stderr by default. This function is safe for concurrent use.

func SetupLoggerWithWriter

func SetupLoggerWithWriter(w io.Writer, debug, structured bool)

SetupLoggerWithWriter configures the logger with a custom writer. This is useful for testing or redirecting logs. This function is safe for concurrent use.

func Warn

func Warn(msg string, args ...any)

Warn logs a warning message with optional key-value pairs.

Example:

logutil.Warn("deprecated API called", "endpoint", "/v1/users")

Types

type ComponentLogger added in v0.5.1

type ComponentLogger struct {
	// contains filtered or unexported fields
}

ComponentLogger provides component-scoped structured logging. It wraps slog.Logger with convenient context chaining.

func NewLogger added in v0.5.1

func NewLogger(component string) *ComponentLogger

NewLogger creates a Logger scoped to a named component.

func (*ComponentLogger) Component added in v0.5.1

func (l *ComponentLogger) Component() string

Component returns the component name for this logger.

func (*ComponentLogger) Debug added in v0.5.1

func (l *ComponentLogger) Debug(msg string, args ...any)

Debug logs a message at debug level.

func (*ComponentLogger) Error added in v0.5.1

func (l *ComponentLogger) Error(msg string, args ...any)

Error logs a message at error level.

func (*ComponentLogger) Info added in v0.5.1

func (l *ComponentLogger) Info(msg string, args ...any)

Info logs a message at info level.

func (*ComponentLogger) Warn added in v0.5.1

func (l *ComponentLogger) Warn(msg string, args ...any)

Warn logs a message at warn level.

func (*ComponentLogger) WithFields added in v0.5.1

func (l *ComponentLogger) WithFields(fields ...any) *ComponentLogger

WithFields returns a new Logger with additional fields. Fields are provided as alternating key-value pairs.

func (*ComponentLogger) WithOperation added in v0.5.1

func (l *ComponentLogger) WithOperation(name string) *ComponentLogger

WithOperation returns a new Logger with the operation context added.

func (*ComponentLogger) WithService added in v0.5.1

func (l *ComponentLogger) WithService(name string) *ComponentLogger

WithService returns a new Logger with the service context added.

type Level

type Level int

Level represents the logging level.

const (
	// LevelDebug is for debug messages.
	LevelDebug Level = iota
	// LevelInfo is for informational messages.
	LevelInfo
	// LevelWarn is for warnings.
	LevelWarn
	// LevelError is for errors.
	LevelError
)

func GetLevel

func GetLevel() Level

GetLevel returns the current logging level. This function is safe for concurrent use.

func ParseLevel

func ParseLevel(s string) Level

ParseLevel parses a string into a Level. Valid values are: "debug", "info", "warn", "warning", "error". Returns LevelInfo for unrecognized values.

Jump to

Keyboard shortcuts

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