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 ComponentLogger
- func (l *ComponentLogger) Component() string
- func (l *ComponentLogger) Debug(msg string, args ...any)
- func (l *ComponentLogger) Error(msg string, args ...any)
- func (l *ComponentLogger) Info(msg string, args ...any)
- func (l *ComponentLogger) Warn(msg string, args ...any)
- func (l *ComponentLogger) WithFields(fields ...any) *ComponentLogger
- func (l *ComponentLogger) WithOperation(name string) *ComponentLogger
- func (l *ComponentLogger) WithService(name string) *ComponentLogger
- 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 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.
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.