logger

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: MIT Imports: 9 Imported by: 0

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
	Data    types.JSONMap[any]
	Message string
	Level   slog.Level
}

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

type Logger added in v1.0.0

type Logger interface {
	Debug(msg string, kv ...any)
	Info(msg string, kv ...any)
	Warn(msg string, kv ...any)
	Error(msg string, kv ...any)

	// With returns a child logger that prepends the given key/value
	// pairs to every record.
	With(kv ...any) Logger
}

Logger is the small structured-logging surface used by Base and its plugins. It deliberately mirrors the geth/luxfi-style variadic API (msg + alternating "key", value pairs) so that the same call sites work with both backends.

Two implementations live in this package:

  • slogAdapter (NewSlog) wraps the standard library's *slog.Logger so that the BatchHandler-based on-disk log storage keeps working.
  • luxfiAdapter (NewLuxfi) wraps github.com/luxfi/log.Logger so that non-storage consumers run through the canonical Lux logger.

Callers should never type-assert to a concrete implementation. For the one historical escape hatch (attaching a custom slog.Handler in dev mode and in tests) use SlogHandler on the slog-backed adapter.

type LuxfiAdapter added in v1.0.0

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

LuxfiAdapter wraps a github.com/luxfi/log.Logger so that it satisfies Logger. The luxfi/log API already uses the geth-style variadic key/value pairs, so the adapter is a thin pass-through.

func NewLuxfi added in v1.0.0

func NewLuxfi(l luxlog.Logger) *LuxfiAdapter

NewLuxfi wraps the given luxfi/log.Logger. Pass nil to fall back to the package default logger.

func (*LuxfiAdapter) Debug added in v1.0.0

func (a *LuxfiAdapter) Debug(msg string, kv ...any)

Debug logs at debug level.

func (*LuxfiAdapter) Error added in v1.0.0

func (a *LuxfiAdapter) Error(msg string, kv ...any)

Error logs at error level.

func (*LuxfiAdapter) Info added in v1.0.0

func (a *LuxfiAdapter) Info(msg string, kv ...any)

Info logs at info level.

func (*LuxfiAdapter) Underlying added in v1.0.0

func (a *LuxfiAdapter) Underlying() luxlog.Logger

Underlying returns the wrapped luxfi/log Logger.

func (*LuxfiAdapter) Warn added in v1.0.0

func (a *LuxfiAdapter) Warn(msg string, kv ...any)

Warn logs at warn level.

func (*LuxfiAdapter) With added in v1.0.0

func (a *LuxfiAdapter) With(kv ...any) Logger

With returns a child logger seeded with the given key/value pairs.

type SlogAdapter added in v1.0.0

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

SlogAdapter wraps a *slog.Logger so that it satisfies Logger. It is the only adapter that exposes the underlying slog.Handler — that escape hatch is required by the in-app log batcher (BatchHandler) and by tests that need to type-assert the handler.

func NewSlog added in v1.0.0

func NewSlog(l *slog.Logger) *SlogAdapter

NewSlog wraps the given *slog.Logger. Pass slog.New(handler) here.

func (*SlogAdapter) Debug added in v1.0.0

func (s *SlogAdapter) Debug(msg string, kv ...any)

Debug logs at debug level.

func (*SlogAdapter) Error added in v1.0.0

func (s *SlogAdapter) Error(msg string, kv ...any)

Error logs at error level.

func (*SlogAdapter) Handler added in v1.0.0

func (s *SlogAdapter) Handler() slog.Handler

Handler returns the underlying slog.Handler, mainly so that BatchHandler can be reached for SetLevel during settings-reload.

func (*SlogAdapter) Info added in v1.0.0

func (s *SlogAdapter) Info(msg string, kv ...any)

Info logs at info level.

func (*SlogAdapter) Slog added in v1.0.0

func (s *SlogAdapter) Slog() *slog.Logger

Slog returns the wrapped *slog.Logger. Reserved for the BatchHandler plumbing — do not call from business logic.

func (*SlogAdapter) Warn added in v1.0.0

func (s *SlogAdapter) Warn(msg string, kv ...any)

Warn logs at warn level.

func (*SlogAdapter) With added in v1.0.0

func (s *SlogAdapter) With(kv ...any) Logger

With returns a child logger seeded with the given key/value pairs.

Jump to

Keyboard shortcuts

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