sinks

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2025 License: MIT Imports: 23 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AsyncMetrics

type AsyncMetrics struct {
	Processed  uint64 // Number of events successfully processed
	Dropped    uint64 // Number of events dropped due to overflow
	Errors     uint64 // Number of errors encountered
	BufferSize int    // Current number of events in buffer
	BufferCap  int    // Buffer capacity
}

AsyncMetrics contains operational metrics for the async sink.

type AsyncOptions

type AsyncOptions struct {
	// BufferSize is the size of the channel buffer for log events.
	BufferSize int

	// OverflowStrategy defines what to do when the buffer is full.
	OverflowStrategy OverflowStrategy

	// FlushInterval is how often to flush events to the wrapped sink.
	// 0 means flush immediately for each event.
	FlushInterval time.Duration

	// BatchSize is the maximum number of events to batch together.
	// 0 means no batching.
	BatchSize int

	// OnError is called when an error occurs in the background worker.
	OnError func(error)

	// ShutdownTimeout is the maximum time to wait for pending events during shutdown.
	ShutdownTimeout time.Duration
}

AsyncOptions configures the async sink wrapper.

type AsyncSink

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

AsyncSink wraps another sink to provide asynchronous, non-blocking logging.

func NewAsyncSink

func NewAsyncSink(wrapped core.LogEventSink, options AsyncOptions) *AsyncSink

NewAsyncSink creates a new async sink wrapper.

func (*AsyncSink) Close

func (as *AsyncSink) Close() error

Close shuts down the async sink and waits for pending events.

func (*AsyncSink) Emit

func (as *AsyncSink) Emit(event *core.LogEvent)

Emit sends a log event to the async buffer.

func (*AsyncSink) GetMetrics

func (as *AsyncSink) GetMetrics() AsyncMetrics

GetMetrics returns metrics about the async sink operation.

func (*AsyncSink) WaitForEmpty

func (as *AsyncSink) WaitForEmpty(ctx context.Context) error

WaitForEmpty blocks until the async buffer is empty or the context is cancelled. This is useful for testing or graceful shutdown scenarios.

type BufferedLogEvent

type BufferedLogEvent struct {
	Event     *core.LogEvent `json:"event"`
	Timestamp int64          `json:"timestamp"`
	Sequence  uint64         `json:"sequence"`
}

BufferedLogEvent represents a persisted log event with metadata.

type Color

type Color string

Color represents an ANSI color code.

const (
	// Basic colors
	ColorReset Color = "\033[0m"
	ColorBold  Color = "\033[1m"
	ColorDim   Color = "\033[2m"

	// Foreground colors
	ColorBlack   Color = "\033[30m"
	ColorRed     Color = "\033[31m"
	ColorGreen   Color = "\033[32m"
	ColorYellow  Color = "\033[33m"
	ColorBlue    Color = "\033[34m"
	ColorMagenta Color = "\033[35m"
	ColorCyan    Color = "\033[36m"
	ColorWhite   Color = "\033[37m"

	// Bright foreground colors
	ColorBrightBlack   Color = "\033[90m"
	ColorBrightRed     Color = "\033[91m"
	ColorBrightGreen   Color = "\033[92m"
	ColorBrightYellow  Color = "\033[93m"
	ColorBrightBlue    Color = "\033[94m"
	ColorBrightMagenta Color = "\033[95m"
	ColorBrightCyan    Color = "\033[96m"
	ColorBrightWhite   Color = "\033[97m"

	// Background colors
	ColorBgBlack   Color = "\033[40m"
	ColorBgRed     Color = "\033[41m"
	ColorBgGreen   Color = "\033[42m"
	ColorBgYellow  Color = "\033[43m"
	ColorBgBlue    Color = "\033[44m"
	ColorBgMagenta Color = "\033[45m"
	ColorBgCyan    Color = "\033[46m"
	ColorBgWhite   Color = "\033[47m"
)

func Ansi256BgColor added in v0.2.0

func Ansi256BgColor(n int) Color

Ansi256BgColor creates an ANSI 256-background color code.

func Ansi256Color added in v0.2.0

func Ansi256Color(n int) Color

Ansi256Color creates an ANSI 256-color code.

type ConsoleSink

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

ConsoleSink writes log events to the console.

func NewConsoleSink

func NewConsoleSink() *ConsoleSink

NewConsoleSink creates a new console sink that writes to stdout.

func NewConsoleSinkWithProperties

func NewConsoleSinkWithProperties() *ConsoleSink

NewConsoleSinkWithProperties creates a new console sink that displays properties.

func NewConsoleSinkWithTemplate added in v0.2.0

func NewConsoleSinkWithTemplate(template string) (*ConsoleSink, error)

NewConsoleSinkWithTemplate creates a new console sink with a custom output template.

func NewConsoleSinkWithTemplateAndTheme added in v0.2.0

func NewConsoleSinkWithTemplateAndTheme(template string, theme *ConsoleTheme) (*ConsoleSink, error)

NewConsoleSinkWithTemplateAndTheme creates a new console sink with both template and theme.

func NewConsoleSinkWithTheme

func NewConsoleSinkWithTheme(theme *ConsoleTheme) *ConsoleSink

NewConsoleSinkWithTheme creates a new console sink with a custom theme.

func NewConsoleSinkWithWriter

func NewConsoleSinkWithWriter(w io.Writer) *ConsoleSink

NewConsoleSinkWithWriter creates a new console sink with a custom writer.

func (*ConsoleSink) Close

func (cs *ConsoleSink) Close() error

Close releases any resources held by the sink.

func (*ConsoleSink) Emit

func (cs *ConsoleSink) Emit(event *core.LogEvent)

Emit writes the log event to the console.

func (*ConsoleSink) EmitSimple

func (cs *ConsoleSink) EmitSimple(timestamp time.Time, level core.LogEventLevel, message string)

EmitSimple writes a simple log message without allocations.

func (*ConsoleSink) SetTheme

func (cs *ConsoleSink) SetTheme(theme *ConsoleTheme)

SetTheme updates the console theme.

func (*ConsoleSink) SetUseColor

func (cs *ConsoleSink) SetUseColor(useColor bool)

SetUseColor enables or disables color output.

func (*ConsoleSink) ShowProperties

func (cs *ConsoleSink) ShowProperties(show bool)

ShowProperties enables or disables property display.

type ConsoleTheme

type ConsoleTheme struct {
	// Level colors
	VerboseColor     Color
	DebugColor       Color
	InformationColor Color
	WarningColor     Color
	ErrorColor       Color
	FatalColor       Color

	// Element colors
	TimestampColor   Color
	MessageColor     Color
	PropertyKeyColor Color
	PropertyValColor Color
	BracketColor     Color // Color for brackets and delimiters

	// Formatting
	LevelFormat     string // Format string for level, e.g., "[%s]" or "%s:"
	TimestampFormat string // Time format string
	PropertyFormat  string // Format for properties, e.g., "%s=%v"
}

ConsoleTheme defines the colors and formatting for console output.

func AutoLiterateTheme added in v0.2.0

func AutoLiterateTheme() *ConsoleTheme

AutoLiterateTheme returns LiterateTheme for 256-color terminals or Literate8ColorTheme otherwise.

func DefaultTheme

func DefaultTheme() *ConsoleTheme

DefaultTheme returns the default console theme.

func DevTheme

func DevTheme() *ConsoleTheme

DevTheme returns a developer-friendly theme with more information.

func LiteTheme

func LiteTheme() *ConsoleTheme

LiteTheme returns a minimalist theme with subtle colors.

func Literate8ColorTheme added in v0.2.0

func Literate8ColorTheme() *ConsoleTheme

Literate8ColorTheme returns a Literate-inspired theme using only basic 8 ANSI colors. This is useful for terminals that don't support 256 colors.

func LiterateTheme added in v0.2.0

func LiterateTheme() *ConsoleTheme

LiterateTheme returns the Serilog Literate theme using ANSI 256 colors. This theme provides readable, non-garish colors that are easy on the eyes.

func NoColorTheme

func NoColorTheme() *ConsoleTheme

NoColorTheme returns a theme without any colors.

func (*ConsoleTheme) GetLevelColor

func (t *ConsoleTheme) GetLevelColor(level core.LogEventLevel) Color

GetLevelColor returns the color for a specific log level.

type DurableOptions

type DurableOptions struct {
	// BufferPath is the directory where buffer files are stored.
	BufferPath string

	// MaxBufferSize is the maximum size in bytes of the buffer file.
	MaxBufferSize int64

	// MaxBufferFiles is the maximum number of buffer files to keep.
	MaxBufferFiles int

	// RetryInterval is how often to attempt delivery to the wrapped sink.
	RetryInterval time.Duration

	// BatchSize is the number of events to process in each retry batch.
	BatchSize int

	// OnError is called when an error occurs in the background worker.
	OnError func(error)

	// ShutdownTimeout is the maximum time to wait during shutdown.
	ShutdownTimeout time.Duration

	// FlushInterval is how often to flush the buffer file to disk.
	FlushInterval time.Duration

	// ChannelBufferSize is the size of the internal event channel.
	// Defaults to 10000 if not specified.
	ChannelBufferSize int
}

DurableOptions configures the durable buffer sink.

type DurableSink

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

DurableSink wraps another sink to provide persistent buffering when the sink fails.

func NewDurableSink

func NewDurableSink(wrapped core.LogEventSink, options DurableOptions) (*DurableSink, error)

NewDurableSink creates a new durable buffer sink.

func (*DurableSink) Close

func (ds *DurableSink) Close() error

Close shuts down the durable sink and flushes remaining events.

func (*DurableSink) Emit

func (ds *DurableSink) Emit(event *core.LogEvent)

Emit sends a log event through the durable buffer.

func (*DurableSink) GetMetrics

func (ds *DurableSink) GetMetrics() map[string]uint64

GetMetrics returns current metrics for monitoring.

func (*DurableSink) IsHealthy

func (ds *DurableSink) IsHealthy() bool

IsHealthy returns true if the wrapped sink is currently healthy.

type ElasticsearchOption

type ElasticsearchOption func(*ElasticsearchSink)

ElasticsearchOption configures an Elasticsearch sink

func WithElasticsearchAPIKey

func WithElasticsearchAPIKey(apiKey string) ElasticsearchOption

WithElasticsearchAPIKey sets the API key for authentication

func WithElasticsearchBasicAuth

func WithElasticsearchBasicAuth(username, password string) ElasticsearchOption

WithElasticsearchBasicAuth sets username and password for basic authentication

func WithElasticsearchBatchSize

func WithElasticsearchBatchSize(size int) ElasticsearchOption

WithElasticsearchBatchSize sets the batch size

func WithElasticsearchBatchTimeout

func WithElasticsearchBatchTimeout(timeout time.Duration) ElasticsearchOption

WithElasticsearchBatchTimeout sets the batch timeout

func WithElasticsearchDataStreams

func WithElasticsearchDataStreams() ElasticsearchOption

WithElasticsearchDataStreams enables data streams instead of indices

func WithElasticsearchIndex

func WithElasticsearchIndex(index string) ElasticsearchOption

WithElasticsearchIndex sets the index name or data stream name

func WithElasticsearchPipeline

func WithElasticsearchPipeline(pipeline string) ElasticsearchOption

WithElasticsearchPipeline sets the ingest pipeline to use

func WithElasticsearchURLs

func WithElasticsearchURLs(urls ...string) ElasticsearchOption

WithElasticsearchURLs sets multiple Elasticsearch URLs for load balancing

type ElasticsearchSink

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

ElasticsearchSink writes log events to Elasticsearch

func NewElasticsearchSink

func NewElasticsearchSink(url string, opts ...ElasticsearchOption) (*ElasticsearchSink, error)

NewElasticsearchSink creates a new Elasticsearch sink

func (*ElasticsearchSink) Close

func (es *ElasticsearchSink) Close() error

Close closes the sink

func (*ElasticsearchSink) Emit

func (es *ElasticsearchSink) Emit(event *core.LogEvent)

Emit writes a log event to the sink

type FileSink

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

FileSink writes log events to a file.

func NewFileSink

func NewFileSink(path string) (*FileSink, error)

NewFileSink creates a new file sink.

func NewFileSinkWithOptions

func NewFileSinkWithOptions(path string, bufferSize int) (*FileSink, error)

NewFileSinkWithOptions creates a new file sink with custom options.

func NewFileSinkWithTemplate added in v0.2.0

func NewFileSinkWithTemplate(path string, template string) (*FileSink, error)

NewFileSinkWithTemplate creates a new file sink with a custom output template.

func (*FileSink) Close

func (fs *FileSink) Close() error

Close flushes and closes the file.

func (*FileSink) Emit

func (fs *FileSink) Emit(event *core.LogEvent)

Emit writes the log event to the file.

func (*FileSink) EmitSimple

func (fs *FileSink) EmitSimple(timestamp time.Time, level core.LogEventLevel, message string)

EmitSimple writes a simple log message without allocations.

type FilteredSink

type FilteredSink[T any] struct {
	// contains filtered or unexported fields
}

FilteredSink applies type-safe filtering

func NewFilteredSink

func NewFilteredSink[T any](innerSink TypedSink[T], predicate func(*core.LogEvent, T) bool) *FilteredSink[T]

NewFilteredSink creates a new filtered sink

func (*FilteredSink[T]) Close

func (s *FilteredSink[T]) Close() error

Close closes the inner sink

func (*FilteredSink[T]) EmitTyped

func (s *FilteredSink[T]) EmitTyped(event *core.LogEvent, data T) error

EmitTyped emits only if predicate returns true

type MemorySink

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

MemorySink stores log events in memory for testing purposes.

func NewMemorySink

func NewMemorySink() *MemorySink

NewMemorySink creates a new memory sink.

func (*MemorySink) Clear

func (m *MemorySink) Clear()

Clear removes all stored events.

func (*MemorySink) Close

func (m *MemorySink) Close() error

Close does nothing for memory sink.

func (*MemorySink) Count

func (m *MemorySink) Count() int

Count returns the number of stored events.

func (*MemorySink) Emit

func (m *MemorySink) Emit(event *core.LogEvent)

Emit stores the event in memory.

func (*MemorySink) Events

func (m *MemorySink) Events() []core.LogEvent

Events returns a copy of all stored events.

func (*MemorySink) FindEvents

func (m *MemorySink) FindEvents(predicate func(*core.LogEvent) bool) []core.LogEvent

FindEvents returns events that match the given predicate.

func (*MemorySink) HasEvent

func (m *MemorySink) HasEvent(predicate func(*core.LogEvent) bool) bool

HasEvent returns true if any event matches the predicate.

func (*MemorySink) LastEvent

func (m *MemorySink) LastEvent() *core.LogEvent

LastEvent returns the most recent event, or nil if no events.

type OverflowStrategy

type OverflowStrategy int

OverflowStrategy defines what to do when the async buffer is full.

const (
	// OverflowBlock blocks the caller until space is available.
	OverflowBlock OverflowStrategy = iota

	// OverflowDrop drops the newest events when the buffer is full.
	OverflowDrop

	// OverflowDropOldest drops the oldest events to make room for new ones.
	OverflowDropOldest
)

type RollingFileOptions

type RollingFileOptions struct {
	// FilePath is the path to the log file.
	FilePath string

	// MaxFileSize is the maximum size of a file before rolling (in bytes).
	// 0 means no size limit.
	MaxFileSize int64

	// RollingInterval defines time-based rolling.
	RollingInterval RollingInterval

	// RetainFileCount is the number of rolled files to keep.
	// 0 means keep all files.
	RetainFileCount int

	// CompressRolledFiles enables gzip compression for rolled files.
	CompressRolledFiles bool

	// Formatter to use for formatting log events.
	Formatter interface {
		Format(event *core.LogEvent) ([]byte, error)
	}

	// BufferSize for file writes (default 64KB).
	BufferSize int
}

RollingFileOptions configures the rolling file sink.

type RollingFileSink

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

RollingFileSink writes log events to files with rolling support.

func NewRollingFileSink

func NewRollingFileSink(options RollingFileOptions) (*RollingFileSink, error)

NewRollingFileSink creates a new rolling file sink.

func (*RollingFileSink) Close

func (rfs *RollingFileSink) Close() error

Close closes the file sink.

func (*RollingFileSink) Emit

func (rfs *RollingFileSink) Emit(event *core.LogEvent)

Emit writes a log event to the file.

type RollingInterval

type RollingInterval int

RollingInterval defines when to roll files based on time.

const (
	// RollingIntervalNone disables time-based rolling.
	RollingIntervalNone RollingInterval = iota
	// RollingIntervalHourly rolls files every hour.
	RollingIntervalHourly
	// RollingIntervalDaily rolls files every day.
	RollingIntervalDaily
	// RollingIntervalWeekly rolls files every week.
	RollingIntervalWeekly
	// RollingIntervalMonthly rolls files every month.
	RollingIntervalMonthly
)

type SeqOption

type SeqOption func(*SeqSink)

SeqOption configures a Seq sink

func WithSeqAPIKey

func WithSeqAPIKey(apiKey string) SeqOption

WithSeqAPIKey sets the API key for authentication

func WithSeqBatchSize

func WithSeqBatchSize(size int) SeqOption

WithSeqBatchSize sets the batch size

func WithSeqBatchTimeout

func WithSeqBatchTimeout(timeout time.Duration) SeqOption

WithSeqBatchTimeout sets the batch timeout

func WithSeqCompression

func WithSeqCompression(enabled bool) SeqOption

WithSeqCompression enables gzip compression

func WithSeqHTTPClient

func WithSeqHTTPClient(client *http.Client) SeqOption

WithSeqHTTPClient sets a custom HTTP client

func WithSeqRetry

func WithSeqRetry(count int, delay time.Duration) SeqOption

WithSeqRetry configures retry behavior

type SeqSink

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

SeqSink writes log events to Seq

func NewSeqSink

func NewSeqSink(serverURL string, opts ...SeqOption) (*SeqSink, error)

NewSeqSink creates a new Seq sink

func (*SeqSink) Close

func (s *SeqSink) Close() error

Close flushes remaining events and stops the sink

func (*SeqSink) Emit

func (s *SeqSink) Emit(event *core.LogEvent)

Emit adds an event to the batch

func (*SeqSink) GetMinimumLevel

func (s *SeqSink) GetMinimumLevel() (core.LogEventLevel, error)

GetMinimumLevel queries Seq for the current minimum level by sending a test event and reading the MinimumLevelAccepted from the response

func (*SeqSink) SeqHealthCheck

func (s *SeqSink) SeqHealthCheck() error

SeqHealthCheck checks if Seq is healthy

type SplunkOption

type SplunkOption func(*SplunkSink)

SplunkOption configures a Splunk sink

func WithSplunkBatchSize

func WithSplunkBatchSize(size int) SplunkOption

WithSplunkBatchSize sets the batch size

func WithSplunkBatchTimeout

func WithSplunkBatchTimeout(timeout time.Duration) SplunkOption

WithSplunkBatchTimeout sets the batch timeout

func WithSplunkHTTPClient

func WithSplunkHTTPClient(client *http.Client) SplunkOption

WithSplunkHTTPClient sets a custom HTTP client

func WithSplunkHost

func WithSplunkHost(host string) SplunkOption

WithSplunkHost sets the host field

func WithSplunkIndex

func WithSplunkIndex(index string) SplunkOption

WithSplunkIndex sets the Splunk index

func WithSplunkSource

func WithSplunkSource(source string) SplunkOption

WithSplunkSource sets the source field

func WithSplunkSourceType

func WithSplunkSourceType(sourceType string) SplunkOption

WithSplunkSourceType sets the sourcetype field

type SplunkSink

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

SplunkSink writes log events to Splunk HTTP Event Collector (HEC)

func NewSplunkSink

func NewSplunkSink(url, token string, opts ...SplunkOption) (*SplunkSink, error)

NewSplunkSink creates a new Splunk sink

func (*SplunkSink) Close

func (s *SplunkSink) Close() error

Close closes the sink

func (*SplunkSink) Emit

func (s *SplunkSink) Emit(event *core.LogEvent)

Emit writes a log event to the sink

type TypedBatchingSink

type TypedBatchingSink[T any] struct {
	// contains filtered or unexported fields
}

TypedBatchingSink batches typed events before emission

func NewTypedBatchingSink

func NewTypedBatchingSink[T any](innerSink TypedSink[T], batchSize int) *TypedBatchingSink[T]

NewTypedBatchingSink creates a new typed batching sink

func (*TypedBatchingSink[T]) Close

func (s *TypedBatchingSink[T]) Close() error

Close flushes and closes the sink

func (*TypedBatchingSink[T]) EmitTyped

func (s *TypedBatchingSink[T]) EmitTyped(event *core.LogEvent, data T) error

EmitTyped adds a typed event to the batch

func (*TypedBatchingSink[T]) Flush

func (s *TypedBatchingSink[T]) Flush() error

Flush sends all batched events

type TypedEvent

type TypedEvent[T any] struct {
	Event *core.LogEvent
	Data  T
}

TypedEvent combines a log event with typed data

type TypedSink

type TypedSink[T any] interface {
	EmitTyped(event *core.LogEvent, typedData T) error
	Close() error
}

TypedSink is a generic sink that can handle specific event types

Jump to

Keyboard shortcuts

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