logging

package
v0.1.0-alpha.6 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package logging provides shared logging utilities for gridctl.

Package logging provides shared logging utilities for gridctl.

Package logging provides shared logging utilities for gridctl.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Caller

func Caller(depth int) (file string, line int)

Caller returns the file and line of the caller at the given depth. Useful for adding source information to logs.

func NewDiscardLogger

func NewDiscardLogger() *slog.Logger

NewDiscardLogger returns a logger that discards all output. This is useful as a default logger when no logging is configured.

func NewStructuredLogger

func NewStructuredLogger(cfg Config) *slog.Logger

NewStructuredLogger creates a new structured logger with the given configuration.

func ParseLevel

func ParseLevel(level string) slog.Level

ParseLevel converts a string level to slog.Level.

func WithComponent

func WithComponent(logger *slog.Logger, component string) *slog.Logger

WithComponent returns a new logger with the given component name.

func WithTraceID

func WithTraceID(logger *slog.Logger, traceID string) *slog.Logger

WithTraceID returns a new logger with the given trace ID.

Types

type BufferHandler

type BufferHandler struct {
	// contains filtered or unexported fields
}

BufferHandler is a slog.Handler that writes to both a LogBuffer and an underlying handler.

func NewBufferHandler

func NewBufferHandler(buffer *LogBuffer, inner slog.Handler) *BufferHandler

NewBufferHandler creates a handler that writes to both a buffer and another handler. If inner is nil, only the buffer receives logs.

func (*BufferHandler) Enabled

func (h *BufferHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level.

func (*BufferHandler) Handle

func (h *BufferHandler) Handle(ctx context.Context, r slog.Record) error

Handle handles the record.

func (*BufferHandler) WithAttrs

func (h *BufferHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new handler with the given attributes.

func (*BufferHandler) WithGroup

func (h *BufferHandler) WithGroup(name string) slog.Handler

WithGroup returns a new handler with the given group name.

type BufferedEntry

type BufferedEntry struct {
	Level     string         `json:"level"`
	Timestamp string         `json:"ts"`
	Message   string         `json:"msg"`
	Component string         `json:"component,omitempty"`
	TraceID   string         `json:"trace_id,omitempty"`
	Attrs     map[string]any `json:"attrs,omitempty"`
}

BufferedEntry represents a log entry stored in the buffer.

type Config

type Config struct {
	// Level sets the minimum log level (default: INFO).
	Level slog.Level
	// Format sets the output format (default: JSON).
	Format LogFormat
	// Output sets the writer for log output (default: os.Stderr).
	Output io.Writer
	// AddSource adds source file and line information to logs.
	AddSource bool
	// Component identifies the logging component (e.g., "gateway", "orchestrator").
	Component string
}

Config holds configuration for structured logging.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a default logging configuration.

type DiscardHandler

type DiscardHandler struct{}

DiscardHandler is a slog.Handler that discards all log records. Use this to create a no-op logger: slog.New(logging.DiscardHandler{})

func (DiscardHandler) Enabled

func (DiscardHandler) Handle

func (DiscardHandler) WithAttrs

func (d DiscardHandler) WithAttrs([]slog.Attr) slog.Handler

func (DiscardHandler) WithGroup

func (d DiscardHandler) WithGroup(string) slog.Handler

type LogBuffer

type LogBuffer struct {
	// contains filtered or unexported fields
}

LogBuffer stores recent log entries in memory for retrieval via API.

func NewLogBuffer

func NewLogBuffer(maxSize int) *LogBuffer

NewLogBuffer creates a new log buffer with the specified maximum size.

func (*LogBuffer) Add

func (b *LogBuffer) Add(entry BufferedEntry)

Add adds a new entry to the buffer.

func (*LogBuffer) Clear

func (b *LogBuffer) Clear()

Clear removes all entries from the buffer.

func (*LogBuffer) Count

func (b *LogBuffer) Count() int

Count returns the number of entries currently in the buffer.

func (*LogBuffer) GetRecent

func (b *LogBuffer) GetRecent(n int) []BufferedEntry

GetRecent returns the most recent n entries.

type LogEntry

type LogEntry struct {
	Level     string         `json:"level"`
	Timestamp string         `json:"ts"`
	Message   string         `json:"msg"`
	Component string         `json:"component,omitempty"`
	TraceID   string         `json:"trace_id,omitempty"`
	Logger    string         `json:"logger,omitempty"`
	Meta      map[string]any `json:"meta,omitempty"`
}

LogEntry represents a structured log entry for frontend consumption. This is the schema that the frontend expects when parsing logs.

type LogFormat

type LogFormat string

LogFormat specifies the output format for structured logging.

const (
	// FormatJSON outputs logs as JSON objects (machine-readable).
	FormatJSON LogFormat = "json"
	// FormatText outputs logs as human-readable text with colors.
	FormatText LogFormat = "text"
)

func ParseFormat

func ParseFormat(format string) LogFormat

ParseFormat converts a string format to LogFormat.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL