logging

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package logging provides structured logging functionality using Go's slog package. It supports both text and JSON output formats, configurable log levels, and context-aware logging for the scanorama application.

Package logging - in-memory ring buffer for capturing and querying log entries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, fields ...any)

Debug logs at debug level using the default logger.

func Error

func Error(msg string, fields ...any)

Error logs at error level using the default logger.

func ErrorDaemon

func ErrorDaemon(msg string, err error, fields ...any)

ErrorDaemon logs daemon-related errors using the default logger.

func ErrorDatabase

func ErrorDatabase(msg string, err error, fields ...any)

ErrorDatabase logs database-related errors using the default logger.

func ErrorDiscovery

func ErrorDiscovery(msg, network string, err error, fields ...any)

ErrorDiscovery logs discovery-related errors using the default logger.

func ErrorScan

func ErrorScan(msg, target string, err error, fields ...any)

ErrorScan logs scan-related errors using the default logger.

func Info

func Info(msg string, fields ...any)

Info logs at info level using the default logger.

func InfoDaemon

func InfoDaemon(msg string, fields ...any)

InfoDaemon logs daemon-related information using the default logger.

func InfoDatabase

func InfoDatabase(msg string, fields ...any)

InfoDatabase logs database-related information using the default logger.

func InfoDiscovery

func InfoDiscovery(msg, network string, fields ...any)

InfoDiscovery logs discovery-related information using the default logger.

func InfoScan

func InfoScan(msg, target string, fields ...any)

InfoScan logs scan-related information using the default logger.

func SetDefault

func SetDefault(logger *Logger)

SetDefault sets the default logger instance.

func TeeHandler added in v0.22.0

func TeeHandler(primary, secondary slog.Handler) slog.Handler

TeeHandler returns a slog.Handler that forwards every record to both primary and secondary. Enabled returns true when either sub-handler is enabled. WithAttrs and WithGroup are propagated to both sub-handlers.

func Warn

func Warn(msg string, fields ...any)

Warn logs at warn level using the default logger.

Types

type Config

type Config struct {
	Level     LogLevel  `yaml:"level" json:"level"`
	Format    LogFormat `yaml:"format" json:"format"`
	Output    string    `yaml:"output" json:"output"`
	AddSource bool      `yaml:"add_source" json:"add_source"`
}

Config holds logging configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a default logging configuration.

type LogEntry added in v0.22.0

type LogEntry struct {
	Time      time.Time         `json:"time"`
	Level     string            `json:"level"`
	Message   string            `json:"message"`
	Component string            `json:"component,omitempty"`
	Attrs     map[string]string `json:"attrs,omitempty"`
}

LogEntry is a single captured log record stored in the ring buffer.

type LogFilter added in v0.22.0

type LogFilter struct {
	Level     string
	Component string
	Search    string
	Since     time.Time
	Until     time.Time
	Page      int
	PageSize  int
}

LogFilter holds the parameters for querying the ring buffer. Zero values mean "no filter" for each field.

type LogFormat

type LogFormat string

LogFormat represents the available log formats.

const (
	FormatText LogFormat = "text"
	FormatJSON LogFormat = "json"
)

type LogLevel

type LogLevel string

LogLevel represents the available log levels.

const (
	LevelDebug LogLevel = "debug"
	LevelInfo  LogLevel = "info"
	LevelWarn  LogLevel = "warn"
	LevelError LogLevel = "error"
)

type Logger

type Logger struct {
	*slog.Logger
	// contains filtered or unexported fields
}

Logger wraps slog.Logger with additional functionality.

func Default

func Default() *Logger

Default returns the default logger instance.

func New

func New(cfg Config) (*Logger, error)

New creates a new structured logger with the given configuration.

func NewDefault

func NewDefault() *Logger

NewDefault creates a logger with default configuration.

func (*Logger) ErrorDaemon

func (l *Logger) ErrorDaemon(msg string, err error, fields ...any)

ErrorDaemon logs daemon-related errors.

func (*Logger) ErrorDatabase

func (l *Logger) ErrorDatabase(msg string, err error, fields ...any)

ErrorDatabase logs database-related errors.

func (*Logger) ErrorDiscovery

func (l *Logger) ErrorDiscovery(msg, network string, err error, fields ...any)

ErrorDiscovery logs discovery-related errors.

func (*Logger) ErrorScan

func (l *Logger) ErrorScan(msg, target string, err error, fields ...any)

ErrorScan logs scan-related errors.

func (*Logger) InfoDaemon

func (l *Logger) InfoDaemon(msg string, fields ...any)

InfoDaemon logs daemon-related information.

func (*Logger) InfoDatabase

func (l *Logger) InfoDatabase(msg string, fields ...any)

InfoDatabase logs database-related information.

func (*Logger) InfoDiscovery

func (l *Logger) InfoDiscovery(msg, network string, fields ...any)

InfoDiscovery logs discovery-related information.

func (*Logger) InfoScan

func (l *Logger) InfoScan(msg, target string, fields ...any)

InfoScan logs scan-related information.

func (*Logger) WithComponent

func (l *Logger) WithComponent(component string) *Logger

WithComponent adds a component field to the logger.

func (*Logger) WithContext

func (l *Logger) WithContext(ctx context.Context) *Logger

WithContext adds context to the logger for structured logging.

func (*Logger) WithError

func (l *Logger) WithError(err error) *Logger

WithError adds an error field to the logger.

func (*Logger) WithFields

func (l *Logger) WithFields(fields ...any) *Logger

WithFields adds structured fields to the logger.

func (*Logger) WithScanID

func (l *Logger) WithScanID(scanID string) *Logger

WithScanID adds a scan ID field to the logger.

func (*Logger) WithTarget

func (l *Logger) WithTarget(target string) *Logger

WithTarget adds a target field to the logger.

type RingBuffer added in v0.22.0

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

RingBuffer is a thread-safe circular buffer of LogEntry values. It also maintains a set of subscriber channels for real-time streaming.

func NewRingBuffer added in v0.22.0

func NewRingBuffer(capacity int) *RingBuffer

NewRingBuffer creates a new RingBuffer with the given capacity. A capacity ≤ 0 uses the default of 2000 entries.

func (*RingBuffer) Append added in v0.22.0

func (rb *RingBuffer) Append(entry LogEntry)

Append writes entry into the ring buffer and notifies all subscribers with a non-blocking send (slow subscribers simply miss the entry).

func (*RingBuffer) Handler added in v0.22.0

func (rb *RingBuffer) Handler() slog.Handler

Handler returns a slog.Handler that appends every handled record to rb.

func (*RingBuffer) Query added in v0.22.0

func (rb *RingBuffer) Query(f LogFilter) (entries []LogEntry, total int)

Query filters the buffer contents and returns a newest-first paginated slice together with the total count before pagination.

Level filtering is minimum-level: "debug" shows everything, "info" hides debug, "warn" hides debug+info, "error" shows only errors. An empty Level string matches all levels.

total is the number of entries that matched the filter (before pagination).

func (*RingBuffer) Recent added in v0.22.0

func (rb *RingBuffer) Recent(n int) []LogEntry

Recent returns up to n entries in chronological order (oldest first). If n exceeds the number of stored entries, all entries are returned.

func (*RingBuffer) Subscribe added in v0.22.0

func (rb *RingBuffer) Subscribe() chan LogEntry

Subscribe returns a buffered channel that receives every future log entry. Call Unsubscribe when done to avoid goroutine / channel leaks.

func (*RingBuffer) Unsubscribe added in v0.22.0

func (rb *RingBuffer) Unsubscribe(ch chan LogEntry)

Unsubscribe removes ch from the subscriber set and closes it so that a range loop over the channel terminates cleanly.

Jump to

Keyboard shortcuts

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