logging

package
v9.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

func WithMinLevel added in v9.0.1

func WithMinLevel(logger *slog.Logger, level slog.Leveler) *slog.Logger

WithMinLevel returns a logger that writes to the same destination as the given logger, but uses the given minimum level.

If the logger uses a LevelHandler, the new level replaces the old level. This is true for loggers from NewLogger. For other loggers, the new level applies in addition to the filtering of the existing handler, so the result can only be more restrictive.

Types

type LevelHandler added in v9.0.1

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

LevelHandler drops records below a minimum level and passes all other records to an inner handler. NewLogger puts a LevelHandler at the outside of its handler chain, and the inner handlers do not filter by level. Because of this, WithMinLevel can give a logger a level that is lower or higher than the level of the logger it comes from.

func NewLevelHandler added in v9.0.1

func NewLevelHandler(inner slog.Handler, level slog.Leveler) *LevelHandler

NewLevelHandler creates a handler that drops records below the given level.

func (*LevelHandler) Enabled added in v9.0.1

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

func (*LevelHandler) Handle added in v9.0.1

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

func (*LevelHandler) WithAttrs added in v9.0.1

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

func (*LevelHandler) WithGroup added in v9.0.1

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

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/protobuf".
	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