Skip to main content

Logger/Zap - Plugin operators

This page lists all operators available in the observability/zap sub-package of ro.

Help improve this documentation

This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.

Your feedback helps us improve!

Installโ€‹

First, import the sub-package in your project:

go get -u github.com/samber/ro/plugins/observability/zap
  • Logs all observable notifications (Next, Error, Complete) using zap logger with formatted messages.

    import (
    "github.com/samber/ro"
    rozap "github.com/samber/ro/plugins/observability/zap"
    "go.uber.org/zap"
    "go.uber.org/zap/zapcore"
    )

    logger := zap.NewExample()
    defer logger.Sync()

    _, _ = ro.Collect(ro.Pipe[int, int](
    ro.Just(1, 2, 3, 4, 5),
    rozap.Log[int](logger, zapcore.InfoLevel),
    ))

    // {"level":"info","msg":"ro.Next: 1"}
    // {"level":"info","msg":"ro.Next: 2"}
    // {"level":"info","msg":"ro.Next: 3"}
    // {"level":"info","msg":"ro.Next: 4"}
    // {"level":"info","msg":"ro.Next: 5"}
    // {"level":"info","msg":"ro.Complete"}
    Prototype:
    func Log[T any](logger *zap.Logger, level zapcore.Level)
  • Logs all observable notifications using zap logger with structured notification data.

    import (
    "github.com/samber/ro"
    rozap "github.com/samber/ro/plugins/observability/zap"
    "go.uber.org/zap"
    "go.uber.org/zap/zapcore"
    )

    logger := zap.NewExample()
    defer logger.Sync()

    _, _ = ro.Collect(ro.Pipe[string, string](
    ro.Just("hello", "world", "golang"),
    rozap.LogWithNotification[string](logger, zapcore.DebugLevel),
    ))

    // {"level":"debug","msg":"ro.Next","value":"hello"}
    // {"level":"debug","msg":"ro.Next","value":"world"}
    // {"level":"debug","msg":"ro.Next","value":"golang"}
    // {"level":"debug","msg":"ro.Complete"}
    Prototype:
    func LogWithNotification[T any](logger *zap.Logger, level zapcore.Level)
  • Terminates the program with a fatal error when an observable error notification occurs using zap logger.

    import (
    "errors"

    "github.com/samber/ro"
    rozap "github.com/samber/ro/plugins/observability/zap"
    "go.uber.org/zap"
    )

    logger := zap.NewExample()

    sub := ro.Pipe[string, string](
    ro.Throw[string](errors.New("critical error")),
    rozap.FatalOnError[string](logger),
    ).Subscribe(ro.NoopObserver[string]())
    defer sub.Unsubscribe()

    // {"level":"fatal","msg":"ro.Error","error":"critical error"}
    // exit status 1
    Prototype:
    func FatalOnError[T any](logger *zap.Logger)