logutil

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 7 Imported by: 1

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 the AZD_DEBUG environment variable to a truthy value

AZD_DEBUG accepts anything strconv.ParseBool accepts, plus "yes", matching the azd extension framework. Unlike the level set through SetupLogger or SetLevel, it is consulted on every call, so it can be flipped at runtime.

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

Component loggers

NewLogger returns a ComponentLogger that tags every line with a component name and supports chaining service, operation, and arbitrary fields:

log := logutil.NewLogger("deploy").WithService("api").WithOperation("push")
log.Info("starting", "revision", rev)

ComponentLogger wraps azdext.Logger and exposes it through AzdextLogger, so a logger created here can be handed to any SDK API that expects one.

Relationship to azdext

Setup delegates to azdext.SetupLogging, but level filtering and the output writer are handled here rather than by the SDK, for two reasons.

azdext.LoggerOptions carries a single Debug boolean, so it can express only debug and info. Delegating the filter would make SetLevel(LevelWarn) and SetLevel(LevelError) silently ineffective.

azdext.NewLogger constructs a fresh handler and defaults to stderr rather than inheriting slog.Default, contrary to its documentation. This package passes the configured writer explicitly; without that, SetOutput would be a no-op for component loggers and their output would leak to stderr.

A ComponentLogger captures the writer and format in effect when it is constructed, so configure logging before creating loggers you intend to capture.

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 azdext.Logger so that a component logger produced here behaves the same as one produced by the SDK and can be handed to any API expecting one, via AzdextLogger.

Level filtering stays here rather than in the SDK logger. azdext.LoggerOptions carries a single Debug boolean, so it can express only debug and info; there is no way to ask it for warn or error. The wrapped logger is therefore built to pass everything through, and the package level set by SetLevel decides what actually gets emitted. Without this, SetLevel(LevelError) would silently keep emitting info lines.

func NewLogger added in v0.5.1

func NewLogger(component string) *ComponentLogger

NewLogger creates a Logger scoped to a named component.

The logger captures the output writer and format in effect at the time of the call. Call SetOutput or SetupLoggerWithWriter before constructing a logger you intend to capture.

func (*ComponentLogger) AzdextLogger added in v0.6.0

func (l *ComponentLogger) AzdextLogger() *azdext.Logger

AzdextLogger returns the wrapped SDK logger, for passing to APIs that accept one. Note that it does not apply the package level, so writing through it bypasses SetLevel.

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) Slogger added in v0.6.0

func (l *ComponentLogger) Slogger() *slog.Logger

Slogger returns the underlying slog.Logger for libraries that accept one. The same caveat as AzdextLogger applies.

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) WithComponent added in v0.6.0

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

WithComponent returns a new Logger under a different component name, recording the current one as parent_component.

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.

There is no azdext equivalent. azdext.Logger.WithComponent reparents the logger under a new component name, which is a different relationship.

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