logging

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FieldProcessID     = "process.id"
	FieldSessionID     = "session.id"
	FieldRequestID     = "request.id"
	FieldClientID      = "client.id"
	FieldClientType    = "client.type"
	FieldClientVersion = "client.version"
	FieldComponent     = "component"
	FieldClientName    = "client_name"
	FieldTraceID       = "trace_id" // Future: OpenTelemetry
)

Standard structured logging field names (avoid magic strings) Uses dot notation for hierarchical namespacing (OpenTelemetry alignment)

View Source
const (
	ComponentMCPServer    = "mcp-server"
	ComponentSSEClient    = "sse-client"
	ComponentDaemonSSE    = "daemon-sse"
	ComponentGraphManager = "graph-manager"
)

Component names

View Source
const (
	HeaderClientID      = "X-Client-ID"
	HeaderClientType    = "X-Client-Type"
	HeaderClientVersion = "X-Client-Version"
)

HTTP headers for client identification

View Source
const (
	// DefaultMaxSize is the default maximum size in megabytes before rotation
	DefaultMaxSize = 10

	// DefaultMaxBackups is the default number of old log files to retain
	DefaultMaxBackups = 3

	// DefaultMaxAge is the default maximum days to retain old log files
	DefaultMaxAge = 28

	// DefaultCompress enables compression of rotated files
	DefaultCompress = true
)

Variables

This section is empty.

Functions

func CreateLogDir

func CreateLogDir(logFilePath string) error

CreateLogDir creates the directory for a log file path

func FromContext

func FromContext(ctx context.Context, fallback *slog.Logger) *slog.Logger

FromContext retrieves logger from context, returns fallback if not found

func IsValidUUIDv7

func IsValidUUIDv7(id string) bool

IsValidUUIDv7 validates UUIDv7 format (for testing)

func NewClientID

func NewClientID() string

NewClientID generates a UUIDv7 for client identification

func NewLogger

func NewLogger(opts ...LoggerOption) (*slog.Logger, *lumberjack.Logger, error)

NewLogger creates a logger with optional rotating file output Returns (*slog.Logger, *lumberjack.Logger, error) The lumberjack.Logger is returned for hot-reload scenarios (can be nil)

func NewProcessID

func NewProcessID() string

NewProcessID generates a UUIDv7 for process identification

func NewRotatingWriter

func NewRotatingWriter(filename string) *lumberjack.Logger

NewRotatingWriter creates a lumberjack.Logger with default rotation settings

func NewRotatingWriterWithConfig

func NewRotatingWriterWithConfig(filename string, cfg RotationConfig) *lumberjack.Logger

NewRotatingWriterWithConfig creates a lumberjack.Logger with custom rotation settings

func NewSessionID

func NewSessionID() string

NewSessionID generates a UUIDv7 for session identification

func ParseLogLevel

func ParseLogLevel(level string) (slog.Level, error)

ParseLogLevel converts string to slog.Level (case-insensitive)

func ProcessIDFromContext

func ProcessIDFromContext(ctx context.Context) (string, bool)

ProcessIDFromContext retrieves process_id from context

func WithClientInfo

func WithClientInfo(logger *slog.Logger, name, version string) *slog.Logger

WithClientInfo enriches logger with client name and version

func WithComponent

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

WithComponent enriches logger with component name

func WithDaemonSSE

func WithDaemonSSE(logger *slog.Logger, clientID, clientType, clientVersion string) *slog.Logger

WithDaemonSSE enriches logger with client.id, client.type, and client.version for daemon SSE

func WithLogger

func WithLogger(ctx context.Context, logger *slog.Logger) context.Context

WithLogger stores logger in context

func WithMCPProcess

func WithMCPProcess(logger *slog.Logger, processID string) *slog.Logger

WithMCPProcess enriches logger with process_id + component for MCP server

func WithProcessID

func WithProcessID(logger *slog.Logger, processID string) *slog.Logger

WithProcessID enriches logger with process_id

func WithProcessIDContext

func WithProcessIDContext(ctx context.Context, processID string) context.Context

WithProcessIDContext stores process_id in context

func WithSSEClient

func WithSSEClient(logger *slog.Logger) *slog.Logger

WithSSEClient enriches logger with component for SSE client

func WithSessionID

func WithSessionID(logger *slog.Logger, sessionID string) *slog.Logger

WithSessionID enriches logger with session_id

Types

type HandlerType

type HandlerType int

HandlerType specifies the slog handler type

const (
	// HandlerJSON creates JSON formatted logs
	HandlerJSON HandlerType = iota
	// HandlerText creates human-readable text logs
	HandlerText
)

type LoggerConfig

type LoggerConfig struct {
	LogFile  string      // Path to log file (empty = stdout/stderr)
	LogLevel string      // "debug", "info", "warn", "error"
	Handler  HandlerType // JSON or Text
	Outputs  []io.Writer // Additional writers (for multi-writer)
}

LoggerConfig holds configuration for logger creation

type LoggerOption

type LoggerOption func(*LoggerConfig)

LoggerOption is a functional option for configuring logger creation

func WithAdditionalOutputs

func WithAdditionalOutputs(writers ...io.Writer) LoggerOption

WithAdditionalOutputs adds additional writers (e.g., stderr for MCP)

func WithHandler

func WithHandler(handler HandlerType) LoggerOption

WithHandler sets the handler type

func WithLogFile

func WithLogFile(path string) LoggerOption

WithLogFile sets the log file path

func WithLogLevel

func WithLogLevel(level string) LoggerOption

WithLogLevel sets the log level

type RotationConfig

type RotationConfig struct {
	MaxSize    int  // megabytes
	MaxBackups int  // number of backups
	MaxAge     int  // days
	Compress   bool // compress rotated files
}

RotationConfig holds lumberjack rotation configuration

func DefaultRotationConfig

func DefaultRotationConfig() RotationConfig

DefaultRotationConfig returns default rotation settings

Jump to

Keyboard shortcuts

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