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
- func Debug(msg string, args ...any)
- func Error(msg string, args ...any)
- func Info(msg string, args ...any)
- func IsDebugEnabled() bool
- func Logger() *slog.Logger
- func SetLevel(level Level)
- func SetOutput(w io.Writer)
- func SetupLogger(debug, structured bool)
- func SetupLoggerWithWriter(w io.Writer, debug, structured bool)
- func Warn(msg string, args ...any)
- type Level
Constants ¶
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 ¶
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 ¶
Error logs an error message with optional key-value pairs.
Example:
logutil.Error("failed to connect", "error", err, "host", dbHost)
func Info ¶
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 ¶
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 ¶
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 ¶
SetupLoggerWithWriter configures the logger with a custom writer. This is useful for testing or redirecting logs. This function is safe for concurrent use.
Types ¶
type Level ¶
type Level int
Level represents the logging level.
func GetLevel ¶
func GetLevel() Level
GetLevel returns the current logging level. This function is safe for concurrent use.
func ParseLevel ¶
ParseLevel parses a string into a Level. Valid values are: "debug", "info", "warn", "warning", "error". Returns LevelInfo for unrecognized values.