logutil

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 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 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