Documentation
¶
Overview ¶
Package logs provides structured logging utilities for Deputy.
This package builds on the standard library's log/slog package, providing a preconfigured logger with support for text and JSON output, colored terminal output, and OpenTelemetry trace context injection.
Creating a Logger ¶
Use New to create a configured logger:
logger := logs.New(logs.Options{
Level: slog.LevelInfo,
Format: "text",
Writer: os.Stderr,
ColorEnabled: true,
})
slog.SetDefault(logger)
Log Levels ¶
Parse log levels from strings:
level, err := logs.ParseLevel("debug") // Returns slog.LevelDebug
level, err := logs.ParseLevel("info") // Returns slog.LevelInfo
level, err := logs.ParseLevel("warn") // Returns slog.LevelWarn
level, err := logs.ParseLevel("error") // Returns slog.LevelError
Output Formats ¶
Two output formats are supported:
- "text": Human-readable format with optional colors
- "json": Structured JSON for log aggregation systems
Trace Context ¶
When OpenTelemetry is enabled, trace context is automatically included:
logger := logs.New(logs.Options{
IncludeTraceContext: true,
ExportToOTel: true,
})
This adds trace_id and span_id to log entries for correlation.
Default Logger ¶
A package-level default logger is available:
logs.SetDefault(logger)
logs.Default().Info("message", "key", "value")
Package logs provides context-aware structured logging for Deputy using slog. It supports attaching loggers to contexts, optional ANSI color formatting, and consistent logging patterns across the codebase.
Index ¶
- func Debug(ctx context.Context, msg string, args ...any)
- func Error(ctx context.Context, msg string, args ...any)
- func FromContext(ctx context.Context) *slog.Logger
- func Info(ctx context.Context, msg string, args ...any)
- func New(opts Options) *slog.Logger
- func ParseLevel(s string) (slog.Level, error)
- func SetDefault(logger *slog.Logger)
- func Warn(ctx context.Context, msg string, args ...any)
- func WithContext(ctx context.Context, logger *slog.Logger) context.Context
- func WithField(ctx context.Context, key string, value any) context.Context
- func WithFields(ctx context.Context, fields map[string]any) context.Context
- type ColorHandler
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromContext ¶
FromContext extracts the logger from the context, returning the default logger if none is found.
func ParseLevel ¶
ParseLevel converts a string level name to slog.Level.
func SetDefault ¶
SetDefault sets the default logger used when no logger is attached to a context.
func WithContext ¶
WithContext returns a new context with the logger attached.
Types ¶
type ColorHandler ¶
type ColorHandler struct {
// contains filtered or unexported fields
}
ColorHandler wraps slog.TextHandler to add ANSI color codes. It delegates all logic to the underlying TextHandler but intercepts the output to inject color codes for the log level.
func NewColorHandler ¶
func NewColorHandler(w io.Writer, opts *slog.HandlerOptions) *ColorHandler
NewColorHandler creates a handler that adds ANSI color codes to log levels.
func (*ColorHandler) Enabled ¶
Enabled reports whether the handler handles records at the given level.
type Options ¶
type Options struct {
// Level sets the minimum log level (Debug, Info, Warn, Error).
Level slog.Level
// Format specifies the output format ("text", "json").
Format string
// Writer is the destination for log output (defaults to os.Stderr).
Writer io.Writer
// ColorEnabled enables ANSI color codes for level and fields (text format only).
ColorEnabled bool
// AddSource includes source file and line number in log output.
AddSource bool
// IncludeTraceContext adds trace_id and span_id to log records when available.
IncludeTraceContext bool
// ExportToOTel enables exporting logs to the OpenTelemetry collector.
// When enabled, logs are sent both to the writer and to the OTel backend.
ExportToOTel bool
// AuditWriter is an optional destination for audit events.
// When set, log messages starting with "deputy." are written as JSON
// to this writer in addition to the main output.
AuditWriter io.Writer
}
Options configures logger behavior including output format and styling.