logger

package
v0.22.19 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2024 License: MIT Imports: 5 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchHandler

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

BatchHandler is a slog handler that writes records on batches.

The log records attributes are formatted in JSON.

Requires the [BatchOptions.WriteFunc] option to be defined.

func NewBatchHandler

func NewBatchHandler(options BatchOptions) *BatchHandler

NewBatchHandler creates a slog compatible handler that writes JSON logs on batches (default to 100), using the given options.

Panics if [BatchOptions.WriteFunc] is not defined.

Example:

l := slog.New(logger.NewBatchHandler(logger.BatchOptions{
    WriteFunc: func(ctx context.Context, logs []*Log) error {
        for _, l := range logs {
            fmt.Println(l.Level, l.Message, l.Data)
        }
        return nil
    }
}))
l.Info("Example message", "title", "lorem ipsum")

func (*BatchHandler) Enabled

func (h *BatchHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level.

The handler ignores records whose level is lower.

func (*BatchHandler) Handle

func (h *BatchHandler) Handle(ctx context.Context, r slog.Record) error

Handle formats the slog.Record argument as JSON object and adds it to the batch queue.

If the batch queue threshold has been reached, the WriteFunc option is invoked with the accumulated logs which in turn will reset the batch queue.

func (*BatchHandler) SetLevel

func (h *BatchHandler) SetLevel(level slog.Level)

SetLevel updates the handler options level to the specified one.

func (*BatchHandler) WithAttrs

func (h *BatchHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new BatchHandler loaded with the specified attributes.

func (*BatchHandler) WithGroup

func (h *BatchHandler) WithGroup(name string) slog.Handler

WithGroup returns a new BatchHandler that starts a group.

All logger attributes will be resolved under the specified group name.

func (*BatchHandler) WriteAll

func (h *BatchHandler) WriteAll(ctx context.Context) error

WriteAll writes all accumulated Log entries and resets the batch queue.

type BatchOptions

type BatchOptions struct {
	// WriteFunc processes the batched logs.
	WriteFunc func(ctx context.Context, logs []*Log) error

	// BeforeAddFunc is optional function that is invoked every time
	// before a new log is added to the batch queue.
	//
	// Return false to skip adding the log into the batch queue.
	BeforeAddFunc func(ctx context.Context, log *Log) bool

	// Level reports the minimum level to log.
	// Levels with lower levels are discarded.
	// If nil, the Handler uses [slog.LevelInfo].
	Level slog.Leveler

	// BatchSize specifies how many logs to accumulate before calling WriteFunc.
	// If not set or 0, fallback to 100 by default.
	BatchSize int
}

BatchOptions are options for the BatchHandler.

type Log

type Log struct {
	Time    time.Time
	Message string
	Level   slog.Level
	Data    types.JsonMap
}

Log is similar to slog.Record bit contains the log attributes as preformatted JSON map.

Jump to

Keyboard shortcuts

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