logger
import logger "ganigeorgiev.com/pocketbase/pocketbase/tools/logger"
Source: tools/logger.
Variables
var BlockKey contextKey
BlockKey is a context key usually used to indicate that the batched logs write should block until writes are completed.
Types
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.
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")
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.
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.
BatchHandler.SetLevel
func (h *BatchHandler) SetLevel(level slog.Level)
SetLevel updates the handler options level to the specified one.
BatchHandler.WithAttrs
func (h *BatchHandler) WithAttrs(attrs []slog.Attr) slog.Handler
WithAttrs returns a new BatchHandler loaded with the specified attributes.
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.
BatchHandler.WriteAll
func (h *BatchHandler) WriteAll(ctx context.Context) error
WriteAll writes all accumulated Log entries and resets the batch queue.
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.
Log
type Log struct {
Time time.Time
Data types.JSONMap[any]
Message string
Level slog.Level
}
Log is similar to slog.Record bit contains the log attributes as preformatted JSON map.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.