logging

package
v9.0.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package logging provides logging-related helpers.

Index

Constants

View Source
const LevelNone = slog.Level(12)

LevelNone is a slog.Level that is higher than all standard levels, effectively disabling all logging when set as the minimum level.

Variables

This section is empty.

Functions

func ContextLoggerMiddleware

func ContextLoggerMiddleware(logger *slog.Logger) func(http.Handler) http.Handler

ContextLoggerMiddleware attaches the given logger to each HTTP request's context.

func GetContextLogger

func GetContextLogger(ctx context.Context) *slog.Logger

GetContextLogger returns the *slog.Logger associated with this HTTP request. If no logger was added to the request context, it returns slog.Default().

func LDLogLevelFromSlog

func LDLogLevelFromSlog(level slog.Level) ldlog.LogLevel

LDLogLevelFromSlog converts a slog.Level to the equivalent ldlog.LogLevel.

func NewEventSourceLogger

func NewEventSourceLogger(logger *slog.Logger) interface {
	Println(...interface{})
	Printf(string, ...interface{})
}

NewEventSourceLogger creates an eventsource-compatible logger that delegates to the given slog.Logger. The eventsource library uses Println/Printf for stream connection lifecycle messages, which are logged at Info level.

func NewLDLogBridge

func NewLDLogBridge(logger *slog.Logger) ldlog.Loggers

NewLDLogBridge creates an ldlog.Loggers instance that delegates all logging to the given slog.Logger. This is used to configure the LaunchDarkly SDK, which requires ldlog.Loggers.

The bridge sets ldlog's minimum level to Debug so that all filtering is controlled by slog's handler. This avoids double-filtering between ldlog and slog.

func NewLogger

func NewLogger(opts ...Option) *slog.Logger

NewLogger creates a *slog.Logger configured for ld-relay.

By default it reads the LOG_FORMAT environment variable to determine format ("json" or "text"). Error-level messages go to stderr; all others go to stdout. If an OTel handler is provided, log records are fanned out to both console and OTel.

func RequestLoggerMiddleware

func RequestLoggerMiddleware(logger *slog.Logger) func(http.Handler) http.Handler

RequestLoggerMiddleware decorates a Handler with debug-level logging of all requests.

func SlogLevelFromLDLog

func SlogLevelFromLDLog(level ldlog.LogLevel) slog.Level

SlogLevelFromLDLog converts an ldlog.LogLevel to the equivalent slog.Level.

func SlogLevelFromString

func SlogLevelFromString(s string) (slog.Level, bool)

SlogLevelFromString parses a log level name (case-insensitive) into a slog.Level. Valid values are "debug", "info", "warn", "error", and "none". Returns the level and true if the string was recognized, or slog.LevelInfo and false otherwise.

Types

type MultiHandler

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

MultiHandler fans out log records to multiple handlers. This is useful for sending logs to both console output and an OTel exporter.

func NewMultiHandler

func NewMultiHandler(handlers ...slog.Handler) *MultiHandler

NewMultiHandler creates a handler that dispatches to all provided handlers.

func (*MultiHandler) Enabled

func (h *MultiHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*MultiHandler) Handle

func (h *MultiHandler) Handle(ctx context.Context, r slog.Record) error

func (*MultiHandler) WithAttrs

func (h *MultiHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*MultiHandler) WithGroup

func (h *MultiHandler) WithGroup(name string) slog.Handler

type OTelLogConfig

type OTelLogConfig struct {
	// Protocol is "grpc" or "http". Defaults to "grpc" if empty.
	Protocol string
}

OTelLogConfig holds configuration for OTel log export.

type OTelLogProvider

type OTelLogProvider struct {
	Provider *sdklog.LoggerProvider
	Handler  slog.Handler
}

OTelLogProvider holds the OTel log provider and the slog handler for use with NewLogger.

func NewOTelLogProvider

func NewOTelLogProvider(cfg OTelLogConfig) (*OTelLogProvider, error)

NewOTelLogProvider creates an OTel LoggerProvider and an otelslog.Handler that can be passed to NewLogger via WithOTelHandler. The caller must call Shutdown on the returned provider when the application exits.

func (*OTelLogProvider) Shutdown

func (p *OTelLogProvider) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the OTel log provider, flushing any pending log records.

type Option

type Option func(*loggerConfig)

Option configures the logger created by NewLogger.

func WithFormat

func WithFormat(format string) Option

WithFormat sets the output format. Valid values are "text" (default) and "json".

func WithLevel

func WithLevel(lv *slog.LevelVar) Option

WithLevel sets the dynamic level variable for the logger. If not provided, a new LevelVar defaulting to slog.LevelInfo is created.

func WithOTelHandler

func WithOTelHandler(h slog.Handler) Option

WithOTelHandler adds an OpenTelemetry slog handler that receives a copy of all log records. When set, log records are sent to both the console handler and the OTel handler.

type StderrSplitHandler

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

StderrSplitHandler routes log records to different handlers based on level. Records at slog.LevelError or above go to the stderr handler; all others go to stdout.

func NewStderrSplitHandler

func NewStderrSplitHandler(stdout, stderr slog.Handler) *StderrSplitHandler

NewStderrSplitHandler creates a handler that splits output between stdout and stderr handlers.

func (*StderrSplitHandler) Enabled

func (h *StderrSplitHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*StderrSplitHandler) Handle

func (h *StderrSplitHandler) Handle(ctx context.Context, r slog.Record) error

func (*StderrSplitHandler) WithAttrs

func (h *StderrSplitHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*StderrSplitHandler) WithGroup

func (h *StderrSplitHandler) WithGroup(name string) slog.Handler

Directories

Path Synopsis
Package logtest provides test utilities for slog-based logging.
Package logtest provides test utilities for slog-based logging.

Jump to

Keyboard shortcuts

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