obs

package
v0.260418.2200 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MPL-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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"
	// 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) 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 RequestBodyEntry added in v0.260418.2200

type RequestBodyEntry struct {
	ID        string // Unique identifier (e.g., "req_1234567890")
	Method    string // HTTP method
	Path      string // Request path
	Body      string // Request body (may be truncated)
	Truncated bool   // True if body was truncated due to size limits
}

RequestBodyEntry represents a stored request body with metadata

type RequestBodyStore added in v0.260418.2200

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

RequestBodyStore stores request bodies in an in-memory circular buffer. Each entry is indexed by a unique ID for retrieval.

This is designed for debugging and troubleshooting: request bodies are stored in memory only (no disk persistence) and automatically discarded when the buffer is full.

func NewRequestBodyStore added in v0.260418.2200

func NewRequestBodyStore(maxSize int) *RequestBodyStore

NewRequestBodyStore creates a new request body store with the specified capacity.

func (*RequestBodyStore) Clear added in v0.260418.2200

func (s *RequestBodyStore) Clear()

Clear removes all entries from the store.

func (*RequestBodyStore) Get added in v0.260418.2200

Get retrieves a request body by ID. Returns nil if the ID is not found (may have been evicted).

func (*RequestBodyStore) Size added in v0.260418.2200

func (s *RequestBodyStore) Size() int

Size returns the current number of stored entries.

func (*RequestBodyStore) Store added in v0.260418.2200

func (s *RequestBodyStore) Store(method, path, body string, maxBodySize int) string

Store stores a request body and returns its unique ID. If the buffer is full, the oldest entry is evicted.

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