streamlog

package
v0.23.3 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: Apache-2.0 Imports: 17 Imported by: 22

Documentation

Overview

Package streamlog provides a non-blocking message broadcaster.

Index

Constants

View Source
const (
	// QueryLogFormatText is the format specifier for text querylog output
	QueryLogFormatText = "text"

	// QueryLogFormatJSON is the format specifier for json querylog output
	QueryLogFormatJSON = "json"

	// QueryLogModeAll is the mode specifier for logging all queries
	QueryLogModeAll = "all"

	// QueryLogModeError is the mode specifier for logging only queries that return an error
	QueryLogModeError = "error"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Formatter

type Formatter interface {
	Logf(io.Writer, url.Values) error
}

Formatter is a simple interface for objects that expose a Format function as needed for streamlog.

type LogFormatter

type LogFormatter func(out io.Writer, params url.Values, message any) error

LogFormatter is the function signature used to format an arbitrary message for the given output writer.

func GetFormatter

func GetFormatter[T any](logger *StreamLogger[T]) LogFormatter

GetFormatter returns a formatter function for objects conforming to the Formatter interface

type QueryLogConfig added in v0.22.0

type QueryLogConfig struct {
	RedactDebugUIQueries bool
	FilterTag            string
	Format               string
	Mode                 string
	RowThreshold         uint64
	TimeThreshold        time.Duration

	EmitOnAnyConditionMet bool
	// contains filtered or unexported fields
}

func GetQueryLogConfig added in v0.22.0

func GetQueryLogConfig() QueryLogConfig

func NewQueryLogConfigForTest added in v0.22.0

func NewQueryLogConfigForTest() QueryLogConfig

func (QueryLogConfig) ShouldEmitLog added in v0.22.0

func (qlConfig QueryLogConfig) ShouldEmitLog(sql string, rowsAffected, rowsReturned uint64, totalTime time.Duration, hasError bool) (bool, string)

ShouldEmitLog returns whether the log with the given SQL query should be emitted or filtered It also returns an EmitReason which is a comma-separated-string to indicate all the conditions triggered for log emit. If both TimeThreshold and FilterTag condition are met, EmitReason will be time,filtertag

type StreamLogger

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

StreamLogger is a non-blocking broadcaster of messages. Subscribers can use channels or HTTP.

func New

func New[T any](name string, size int) *StreamLogger[T]

New returns a new StreamLogger that can stream events to subscribers. The size parameter defines the channel size for the subscribers.

func (*StreamLogger[T]) LogToFile

func (logger *StreamLogger[T]) LogToFile(path string, logf LogFormatter) (chan T, error)

LogToFile starts logging to the specified file path and will reopen the file in response to SIGUSR2.

Returns the channel used for the subscription which can be used to close it.

func (*StreamLogger[T]) Name

func (logger *StreamLogger[T]) Name() string

Name returns the name of StreamLogger.

func (*StreamLogger[T]) Send

func (logger *StreamLogger[T]) Send(message T)

Send sends message to all the writers subscribed to logger. Calling Send does not block.

func (*StreamLogger[T]) ServeLogs

func (logger *StreamLogger[T]) ServeLogs(url string, logf LogFormatter)

ServeLogs registers the URL on which messages will be broadcast. It is safe to register multiple URLs for the same StreamLogger.

func (*StreamLogger[T]) Subscribe

func (logger *StreamLogger[T]) Subscribe(name string) chan T

Subscribe returns a channel which can be used to listen for messages.

func (*StreamLogger[T]) Unsubscribe

func (logger *StreamLogger[T]) Unsubscribe(ch chan T)

Unsubscribe removes the channel from the subscription.

Jump to

Keyboard shortcuts

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