Documentation ¶
Index ¶
- type BatchHandler
- func (h *BatchHandler) Enabled(ctx context.Context, level slog.Level) bool
- func (h *BatchHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *BatchHandler) SetLevel(level slog.Level)
- func (h *BatchHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *BatchHandler) WithGroup(name string) slog.Handler
- func (h *BatchHandler) WriteAll(ctx context.Context) error
- type BatchOptions
- type Log
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 ¶
Enabled reports whether the handler handles records at the given level.
The handler ignores records whose level is lower.
func (*BatchHandler) Handle ¶
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.
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.