flog

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package flog wraps *slog.Logger with a zerolog-style fluent API while preserving slog's handler ecosystem.

The underlying transport remains a *slog.Logger, so any slog.Handler — the stdlib JSON/Text handlers, the phuslu and zerolog adapters under integrations/log/, or any third-party Handler — works unchanged. Callers pick the backend by passing the appropriate *slog.Logger via fastconf.WithLogger.

Usage:

log := flog.New(slog.Default())
log.Info().
    Str("reason", reason).
    Uint64("generation", gen).
    Int("layers", n).
    Err(err).
    Msg("reload swap")

At a disabled level Info()/Debug()/etc. return nil and every fluent method short-circuits on nil, so the chain costs only the level check. Events are pooled, so each emit is amortized zero-allocation aside from the slog.Record path inside the chosen Handler.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

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

Context is the builder returned by Logger.With(); call Logger() to materialize a derived *Logger that attaches the accumulated attrs to every record it emits.

func (*Context) Any

func (c *Context) Any(k string, v any) *Context

func (*Context) Bool

func (c *Context) Bool(k string, v bool) *Context

func (*Context) Dur

func (c *Context) Dur(k string, v time.Duration) *Context

func (*Context) Group

func (c *Context) Group(name string) *Context

Group nests subsequent records under name (slog.Logger.WithGroup semantics).

func (*Context) Int

func (c *Context) Int(k string, v int) *Context

func (*Context) Int64

func (c *Context) Int64(k string, v int64) *Context

func (*Context) Logger

func (c *Context) Logger() *Logger

Logger seals the chain and returns the derived *Logger.

func (*Context) Str

func (c *Context) Str(k, v string) *Context

func (*Context) Time

func (c *Context) Time(k string, v time.Time) *Context

func (*Context) Uint64

func (c *Context) Uint64(k string, v uint64) *Context

type Event

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

Event is a level-gated, attribute-accumulating builder. A nil *Event is a no-op — every fluent method tolerates nil so disabled-level chains short-circuit after the initial level check.

func (*Event) Any

func (e *Event) Any(k string, v any) *Event

Any falls back to slog.Any for slices, maps, or arbitrary values.

func (*Event) Attr

func (e *Event) Attr(a slog.Attr) *Event

Attr passes a pre-built slog.Attr (e.g. slog.Group(...)) into the chain.

func (*Event) Bool

func (e *Event) Bool(k string, v bool) *Event

func (*Event) Dur

func (e *Event) Dur(k string, v time.Duration) *Event

Dur attaches a time.Duration. Stdlib slog renders it as a nanosecond int64 by default; the phuslu and zerolog adapters render it in their native duration form.

func (*Event) Err

func (e *Event) Err(err error) *Event

Err attaches an error under the conventional key "err". Nil errors are silently skipped so chains can stay flat (.Err(err) instead of an if).

func (*Event) Float64

func (e *Event) Float64(k string, v float64) *Event

func (*Event) Int

func (e *Event) Int(k string, v int) *Event

func (*Event) Int64

func (e *Event) Int64(k string, v int64) *Event

func (*Event) Msg

func (e *Event) Msg(msg string)

Msg emits the event with msg and returns the *Event to the pool. After Msg() the receiver must not be reused.

func (*Event) NamedErr

func (e *Event) NamedErr(k string, err error) *Event

NamedErr attaches an error under a caller-chosen key. Nil errors are skipped, matching Err.

func (*Event) Send

func (e *Event) Send()

Send is shorthand for Msg("").

func (*Event) Str

func (e *Event) Str(k, v string) *Event

func (*Event) Strs

func (e *Event) Strs(k string, v []string) *Event

func (*Event) Time

func (e *Event) Time(k string, v time.Time) *Event

func (*Event) Uint64

func (e *Event) Uint64(k string, v uint64) *Event

type Logger

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

Logger wraps a *slog.Logger with fluent builder methods. A zero-value Logger is invalid; use New().

func New

func New(s *slog.Logger) *Logger

New wraps an existing *slog.Logger. If s is nil, slog.Default() is used.

func (*Logger) At

func (l *Logger) At(lvl slog.Level) *Event

At starts an event at an arbitrary slog.Level (use for custom levels).

func (*Logger) AtCtx

func (l *Logger) AtCtx(ctx context.Context, lvl slog.Level) *Event

AtCtx starts an event at an arbitrary slog.Level carrying ctx.

func (*Logger) Debug

func (l *Logger) Debug() *Event

Debug starts a Debug-level event. Returns nil if the level is disabled.

func (*Logger) DebugCtx

func (l *Logger) DebugCtx(ctx context.Context) *Event

DebugCtx starts a Debug-level event carrying ctx.

func (*Logger) Error

func (l *Logger) Error() *Event

Error starts an Error-level event.

func (*Logger) ErrorCtx

func (l *Logger) ErrorCtx(ctx context.Context) *Event

ErrorCtx starts an Error-level event carrying ctx.

func (*Logger) Info

func (l *Logger) Info() *Event

Info starts an Info-level event.

func (*Logger) InfoCtx

func (l *Logger) InfoCtx(ctx context.Context) *Event

InfoCtx starts an Info-level event carrying ctx.

func (*Logger) Slog

func (l *Logger) Slog() *slog.Logger

Slog returns the underlying *slog.Logger for interop with slog-typed APIs.

func (*Logger) Warn

func (l *Logger) Warn() *Event

Warn starts a Warn-level event.

func (*Logger) WarnCtx

func (l *Logger) WarnCtx(ctx context.Context) *Event

WarnCtx starts a Warn-level event carrying ctx.

func (*Logger) With

func (l *Logger) With() *Context

With opens a sub-logger builder. Use at startup or per request — each step allocates a new *slog.Logger, so With() is not a hot-path API.

Jump to

Keyboard shortcuts

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