obs

package
v0.260806.1 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: MPL-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithRequestID added in v0.260604.1

func ContextWithRequestID(ctx context.Context, id string) context.Context

ContextWithRequestID returns a child context carrying the correlation id. Request-scoped code logs via logrus.WithContext(ctx); the MultiLogger hook reads the id back with RequestIDFromContext to route the entry to the model_request source and stamp it with the id.

func RequestIDFromContext added in v0.260604.1

func RequestIDFromContext(ctx context.Context) string

RequestIDFromContext returns the correlation id stored in ctx, or "".

Types

type LogEntry

type LogEntry struct {
	Time    time.Time              `json:"time"`
	Level   string                 `json:"level"`
	Message string                 `json:"message"`
	Source  string                 `json:"source,omitempty"` // Log source identification
	Fields  map[string]interface{} `json:"fields,omitempty"`
}

LogEntry represents a structured log entry for JSON output

type LogSource

type LogSource string

LogSource identifies the source of a log entry for routing decisions

const (
	// LogSourceHTTP indicates logs from HTTP requests
	LogSourceHTTP LogSource = "http"
	// LogSourceSystem indicates logs from the application/system
	LogSourceSystem LogSource = "system"
	// LogSourceAction indicates logs from user actions/operations
	LogSourceAction LogSource = "action"
	// LogSourceSmartRouting indicates logs from smart routing rule evaluation
	LogSourceSmartRouting LogSource = "smart_routing"
	// LogSourceModelRequest indicates request-scoped logs from the model
	// request pipeline (protocol conversion, upstream client calls). These
	// are correlated by a request_id field rather than by transport path.
	LogSourceModelRequest LogSource = "model_request"
	// LogSourceUnknown indicates unknown log source
	LogSourceUnknown LogSource = "unknown"
)

type MemoryLogHook

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

MemoryLogHook is a logrus hook that stores log entries in an in-memory circular buffer.

func NewMemoryLogHook

func NewMemoryLogHook(maxEntries int) *MemoryLogHook

NewMemoryLogHook creates a new memory log hook with the specified maximum capacity.

func (*MemoryLogHook) AddWriter

func (h *MemoryLogHook) AddWriter(w io.Writer)

AddWriter adds a writer for tee output functionality.

func (*MemoryLogHook) Clear

func (h *MemoryLogHook) Clear()

Clear removes all log entries.

func (*MemoryLogHook) Fire

func (h *MemoryLogHook) Fire(entry *logrus.Entry) error

Fire processes each log entry.

func (*MemoryLogHook) GetEntries

func (h *MemoryLogHook) GetEntries() []*logrus.Entry

GetEntries returns all log entries in chronological order.

func (*MemoryLogHook) GetEntriesByLevel

func (h *MemoryLogHook) GetEntriesByLevel(level logrus.Level) []*logrus.Entry

GetEntriesByLevel returns log entries matching the specified level.

func (*MemoryLogHook) GetEntriesByLevelRange

func (h *MemoryLogHook) GetEntriesByLevelRange(minLevel, maxLevel logrus.Level) []*logrus.Entry

GetEntriesByLevelRange returns log entries within the specified level range.

func (*MemoryLogHook) GetEntriesSince

func (h *MemoryLogHook) GetEntriesSince(since time.Time) []*logrus.Entry

GetEntriesSince returns log entries after the specified time.

func (*MemoryLogHook) GetLatest

func (h *MemoryLogHook) GetLatest(n int) []*logrus.Entry

GetLatest returns the newest N log entries.

func (*MemoryLogHook) Levels

func (h *MemoryLogHook) Levels() []logrus.Level

Levels returns the log levels this hook processes.

func (*MemoryLogHook) Size

func (h *MemoryLogHook) Size() int

Size returns the current number of stored log entries.

type MemorySinkConfig

type MemorySinkConfig struct {
	MaxEntries int // Maximum entries to keep in memory (default varies by source)
}

MemorySinkConfig holds configuration for a memory sink

type MultiLogger

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

MultiLogger writes logs to multiple outputs: text, JSON, and memory. Text logs are for human readability, JSON logs for persistence, and memory for quick API access.

Architecture:

┌─────────────────────────────────────────────────────────┐
│                     MultiLogger                         │
│  ┌────────────┐  ┌────────────┐  ┌──────────────────┐  │
│  │    Text    │  │    JSON    │  │  Memory Sinks    │  │
│  │   Output   │  │   Output   │  │  (by source)     │  │
│  └────────────┘  └────────────┘  ├──────────────────┤  │
│                                   │ HTTP (1000)      │  │
│                                   │ System (500)     │  │
│                                   │ Action (100)     │  │
│                                   └──────────────────┘  │
└─────────────────────────────────────────────────────────┘

Usage:

// Get the main multi logger
logger := obs.NewMultiLogger(config)

// Get a scoped logger for specific use case
actionLogger := logger.WithSource(obs.LogSourceAction)
actionLogger.LogAction("add_provider", details, true, "success")

// Or get the underlying logrus logger for custom use
httpLogger := logger.GetLogrusLogger(obs.LogSourceHTTP)
httpLogger.WithField("path", "/api/test").Info("request processed")

func NewMultiLogger

func NewMultiLogger(cfg *MultiLoggerConfig) (*MultiLogger, error)

NewMultiLogger creates a new multi-mode logger

func (*MultiLogger) Close

func (m *MultiLogger) Close() error

Close closes the logger

func (*MultiLogger) GetJSONLogPath

func (m *MultiLogger) GetJSONLogPath() string

GetJSONLogPath returns the path to the JSON log file

func (*MultiLogger) GetLevel

func (m *MultiLogger) GetLevel() logrus.Level

GetLevel returns the current minimum log level

func (*MultiLogger) GetLogrusLogger

func (m *MultiLogger) GetLogrusLogger(source LogSource) *logrus.Logger

GetLogrusLogger returns a logrus.Logger instance that writes to this MultiLogger with the specified source. The logger is cached for performance.

Example:

logger := multiLogger.GetLogrusLogger(LogSourceHTTP)
logger.WithField("path", "/api/test").Info("request processed")

func (*MultiLogger) GetMemorySink

func (m *MultiLogger) GetMemorySink(source LogSource) *MemoryLogHook

GetMemorySink returns the memory sink for the specified source, creating it if necessary. Returns nil if the source has no memory sink configured.

func (*MultiLogger) ReadJSONLogs

func (m *MultiLogger) ReadJSONLogs(limit int) ([]LogEntry, error)

ReadJSONLogs reads log entries from the JSON log file with optional filtering

func (*MultiLogger) ReadJSONLogsBySource added in v0.260604.1

func (m *MultiLogger) ReadJSONLogsBySource(limit int, allowed ...LogSource) ([]LogEntry, error)

ReadJSONLogsBySource reads recent log entries from the JSON log file keeping only entries whose source is in the allowed set. An empty/absent source on a legacy entry is treated as LogSourceSystem. Reading stops once limit matching entries are collected.

func (*MultiLogger) SetLevel

func (m *MultiLogger) SetLevel(level logrus.Level)

SetLevel sets the minimum log level for all loggers

func (*MultiLogger) WithSource

func (m *MultiLogger) WithSource(source LogSource) *ScopedLogger

WithSource returns a scoped logger for the specified source. This provides a convenient API for logging with a specific source.

Example:

actionLogger := logger.WithSource(LogSourceAction)
actionLogger.LogAction("add_provider", details, true, "success")

func (*MultiLogger) Write

func (m *MultiLogger) Write(p []byte) (n int, err error)

Write implements io.Writer for text output

func (*MultiLogger) WriteEntry

func (m *MultiLogger) WriteEntry(entry *logrus.Entry) error

WriteEntry writes a structured log entry to the JSON log and memory sink

type MultiLoggerConfig

type MultiLoggerConfig struct {
	// Text log config (for humans)
	TextLogPath    string
	TextMaxSize    int // MB
	TextMaxBackups int
	TextMaxAge     int // days
	TextCompress   bool

	// JSON log config (for frontend/API)
	JSONLogPath    string
	JSONMaxSize    int // MB (small, for recent logs only)
	JSONMaxBackups int // small number, we only keep recent
	JSONMaxAge     int // days

	// Memory sink config (optional, uses defaults if not specified)
	MemorySinkConfig map[LogSource]MemorySinkConfig
}

MultiLoggerConfig holds configuration for multi-mode logging

func DefaultMultiLoggerConfig

func DefaultMultiLoggerConfig(configDir string) *MultiLoggerConfig

DefaultMultiLoggerConfig returns default configuration

type MultiLoggerHook

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

MultiLoggerHook is a logrus hook that writes to the multi-mode logger This is used for global logrus integration (e.g., main application logs)

func NewMultiLoggerHook

func NewMultiLoggerHook(logger *MultiLogger, levels []logrus.Level) *MultiLoggerHook

NewMultiLoggerHook creates a new multi logger hook for global use Use this for adding the MultiLogger to the global logrus instance

func (*MultiLoggerHook) Fire

func (h *MultiLoggerHook) Fire(entry *logrus.Entry) error

Fire processes each log entry, writing it to the JSON log

func (*MultiLoggerHook) Levels

func (h *MultiLoggerHook) Levels() []logrus.Level

Levels returns the log levels this hook processes

type ScopedLogger

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

ScopedLogger provides a convenient API for logging with a specific source

func (*ScopedLogger) ClearMemory

func (s *ScopedLogger) ClearMemory()

ClearMemory clears all log entries from memory for this scope

func (*ScopedLogger) GetLogrusLogger

func (s *ScopedLogger) GetLogrusLogger() *logrus.Logger

GetLogrusLogger returns the logrus logger for this scope

func (*ScopedLogger) GetMemoryEntries

func (s *ScopedLogger) GetMemoryEntries() []*logrus.Entry

GetMemoryEntries returns all log entries from memory for this scope

func (*ScopedLogger) GetMemoryLatest

func (s *ScopedLogger) GetMemoryLatest(n int) []*logrus.Entry

GetMemoryLatest returns the newest N log entries from memory for this scope

func (*ScopedLogger) GetMemorySink

func (s *ScopedLogger) GetMemorySink() *MemoryLogHook

GetMemorySink returns the memory sink for this scope

func (*ScopedLogger) LogAction added in v0.260414.2000

func (s *ScopedLogger) LogAction(action string, details map[string]interface{}, success bool, message string)

LogAction logs a user action with structured data This is a convenience method for logging user actions to the action source

Parameters:

  • action: The action type (e.g., "add_provider", "delete_provider")
  • details: Additional details about the action (can be nil)
  • success: Whether the action was successful
  • message: A human-readable message about the action

Jump to

Keyboard shortcuts

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