Skip to main content

Logger/slog - Plugin operators

This page lists all operators available in the observability/slog 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/slog
  • Logs with structured logging.

    import (
    "log/slog"
    "os"

    "github.com/samber/ro"
    roslog "github.com/samber/ro/plugins/observability/slog"
    )

    logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

    obs := ro.Pipe[string, string](
    ro.Just("operation 1", "operation 2"),
    roslog.Log[string](logger, slog.LevelInfo),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // time=2009-11-10T23:00:00.000Z level=INFO msg="ro.Next: operation 1"
    // Next: operation 1
    // time=2009-11-10T23:00:00.000Z level=INFO msg="ro.Next: operation 2"
    // Next: operation 2
    // time=2009-11-10T23:00:00.000Z level=INFO msg=ro.Complete
    // Completed
    Prototype:
    func Log[T any](logger slog.Logger, level slog.Level)
  • Logs each notification (Next, Error, Complete) emitted by the source Observable using structured logging with slog.

    import (
    "log/slog"
    "os"

    "github.com/samber/ro"
    roslog "github.com/samber/ro/plugins/observability/slog"
    )

    logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

    obs := ro.Pipe[string, string](
    ro.Just("operation 1", "operation 2"),
    roslog.LogWithNotification[string](logger, slog.LevelInfo),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // time=2009-11-10T23:00:00.000Z level=INFO msg=ro.Next value="operation 1"
    // Next: operation 1
    // time=2009-11-10T23:00:00.000Z level=INFO msg=ro.Next value="operation 2"
    // Next: operation 2
    // time=2009-11-10T23:00:00.000Z level=INFO msg=ro.Complete
    // Completed
    Similar:
    Prototype:
    func LogWithNotification[T any](logger slog.Logger, level slog.Level)