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 ¶
- func Debug(msg string, fields ...any)
- func Error(msg string, fields ...any)
- func ErrorDaemon(msg string, err error, fields ...any)
- func ErrorDatabase(msg string, err error, fields ...any)
- func ErrorDiscovery(msg, network string, err error, fields ...any)
- func ErrorScan(msg, target string, err error, fields ...any)
- func Info(msg string, fields ...any)
- func InfoDaemon(msg string, fields ...any)
- func InfoDatabase(msg string, fields ...any)
- func InfoDiscovery(msg, network string, fields ...any)
- func InfoScan(msg, target string, fields ...any)
- func SetDefault(logger *Logger)
- func TeeHandler(primary, secondary slog.Handler) slog.Handler
- func Warn(msg string, fields ...any)
- type Config
- type LogEntry
- type LogFilter
- type LogFormat
- type LogLevel
- type Logger
- func (l *Logger) ErrorDaemon(msg string, err error, fields ...any)
- func (l *Logger) ErrorDatabase(msg string, err error, fields ...any)
- func (l *Logger) ErrorDiscovery(msg, network string, err error, fields ...any)
- func (l *Logger) ErrorScan(msg, target string, err error, fields ...any)
- func (l *Logger) InfoDaemon(msg string, fields ...any)
- func (l *Logger) InfoDatabase(msg string, fields ...any)
- func (l *Logger) InfoDiscovery(msg, network string, fields ...any)
- func (l *Logger) InfoScan(msg, target string, fields ...any)
- func (l *Logger) WithComponent(component string) *Logger
- func (l *Logger) WithContext(ctx context.Context) *Logger
- func (l *Logger) WithError(err error) *Logger
- func (l *Logger) WithFields(fields ...any) *Logger
- func (l *Logger) WithScanID(scanID string) *Logger
- func (l *Logger) WithTarget(target string) *Logger
- type RingBuffer
- func (rb *RingBuffer) Append(entry LogEntry)
- func (rb *RingBuffer) Handler() slog.Handler
- func (rb *RingBuffer) Query(f LogFilter) (entries []LogEntry, total int)
- func (rb *RingBuffer) Recent(n int) []LogEntry
- func (rb *RingBuffer) Subscribe() chan LogEntry
- func (rb *RingBuffer) Unsubscribe(ch chan LogEntry)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorDaemon ¶
ErrorDaemon logs daemon-related errors using the default logger.
func ErrorDatabase ¶
ErrorDatabase logs database-related errors using the default logger.
func ErrorDiscovery ¶
ErrorDiscovery logs discovery-related errors using the default logger.
func InfoDaemon ¶
InfoDaemon logs daemon-related information using the default logger.
func InfoDatabase ¶
InfoDatabase logs database-related information using the default logger.
func InfoDiscovery ¶
InfoDiscovery logs discovery-related information using the default logger.
func TeeHandler ¶ added in v0.22.0
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.
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 Logger ¶
Logger wraps slog.Logger with additional functionality.
func (*Logger) ErrorDaemon ¶
ErrorDaemon logs daemon-related errors.
func (*Logger) ErrorDatabase ¶
ErrorDatabase logs database-related errors.
func (*Logger) ErrorDiscovery ¶
ErrorDiscovery logs discovery-related errors.
func (*Logger) InfoDaemon ¶
InfoDaemon logs daemon-related information.
func (*Logger) InfoDatabase ¶
InfoDatabase logs database-related information.
func (*Logger) InfoDiscovery ¶
InfoDiscovery logs discovery-related information.
func (*Logger) WithComponent ¶
WithComponent adds a component field to the logger.
func (*Logger) WithContext ¶
WithContext adds context to the logger for structured logging.
func (*Logger) WithFields ¶
WithFields adds structured fields to the logger.
func (*Logger) WithScanID ¶
WithScanID adds a scan ID field to the logger.
func (*Logger) WithTarget ¶
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.