Documentation
¶
Overview ¶
Package logging provides logging-related helpers.
Index ¶
- Constants
- func ContextLoggerMiddleware(logger *slog.Logger) func(http.Handler) http.Handler
- func GetContextLogger(ctx context.Context) *slog.Logger
- func LDLogLevelFromSlog(level slog.Level) ldlog.LogLevel
- func NewEventSourceLogger(logger *slog.Logger) interface{ ... }
- func NewLDLogBridge(logger *slog.Logger) ldlog.Loggers
- func NewLogger(opts ...Option) *slog.Logger
- func RequestLoggerMiddleware(logger *slog.Logger) func(http.Handler) http.Handler
- func SlogLevelFromLDLog(level ldlog.LogLevel) slog.Level
- func SlogLevelFromString(s string) (slog.Level, bool)
- type MultiHandler
- type OTelLogConfig
- type OTelLogProvider
- type Option
- type StderrSplitHandler
Constants ¶
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 ¶
ContextLoggerMiddleware attaches the given logger to each HTTP request's context.
func GetContextLogger ¶
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 ¶
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 ¶
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 ¶
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 ¶
RequestLoggerMiddleware decorates a Handler with debug-level logging of all requests.
func SlogLevelFromLDLog ¶
SlogLevelFromLDLog converts an ldlog.LogLevel to the equivalent slog.Level.
func SlogLevelFromString ¶
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.
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.
type Option ¶
type Option func(*loggerConfig)
Option configures the logger created by NewLogger.
func WithFormat ¶
WithFormat sets the output format. Valid values are "text" (default) and "json".
func WithLevel ¶
WithLevel sets the dynamic level variable for the logger. If not provided, a new LevelVar defaulting to slog.LevelInfo is created.
func WithOTelHandler ¶
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.