Documentation
¶
Index ¶
- func DisableScopeGlobal(scopes ...*Scope)
- func EnableScope(ctx context.Context, scopes ...*Scope) context.Context
- func EnableScopeGlobal(scopes ...*Scope)
- func From(ctx context.Context, options ...Option) *slog.Logger
- func With(ctx context.Context, logger *slog.Logger) context.Context
- type Capture
- type Option
- type Scope
- type ScopeOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DisableScopeGlobal ¶
func DisableScopeGlobal(scopes ...*Scope)
DisableScopeGlobal disables the given scopes globally
func EnableScope ¶
EnableScope returns a new context with the given scopes enabled
func EnableScopeGlobal ¶
func EnableScopeGlobal(scopes ...*Scope)
EnableScopeGlobal dynamically enables the given scopes globally
Types ¶
type Capture ¶
type Capture struct {
// contains filtered or unexported fields
}
Capture holds captured log records for testing.
func NewCapture ¶
NewCapture creates a new context with log capture capability.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option represents configuration options for logger creation
func WithFastRand ¶
func WithFastRand() Option
WithFastRand creates an option to use fast pseudo-random numbers for sampling instead of cryptographically secure random numbers for better performance
func WithSampling ¶
WithSampling creates an option to enable probabilistic logging
type Scope ¶
type Scope struct {
// contains filtered or unexported fields
}
Scope represents a logging scope with hierarchical support
func GetGlobalEnabledScopes ¶
func GetGlobalEnabledScopes() []*Scope
GetGlobalEnabledScopes returns the globally enabled scopes
func NewScope ¶
func NewScope(name string, options ...ScopeOption) *Scope
NewScope creates a new scope with the given name and options.
Scope activation behavior: Multiple options are combined with OR logic - if ANY condition is met, the scope is active. Available activation conditions: 1. Dynamic enablement via EnableScope(ctx, scope) or EnableScopeGlobal(scope) 2. Parent scope activation (children inherit parent's active state) 3. Environment variable existence (via EnabledBy option)
Combined options examples:
Example 1: Multiple environment variables
scope := ctxlog.NewScope("api", ctxlog.EnabledBy("DEBUG_API", "TRACE_API", "DEV_MODE"))
// Active if ANY of DEBUG_API, TRACE_API, or DEV_MODE is set
Example 2: Single environment variable
scope := ctxlog.NewScope("debug", ctxlog.EnabledBy("DEBUG_MODE"))
// Active if DEBUG_MODE env var is set (any value, even empty)
Example 3: No options (manual activation only)
scope := ctxlog.NewScope("manual")
// Only active via EnableScope(ctx, scope) or EnableScopeGlobal(scope)
type ScopeOption ¶
type ScopeOption func(*scopeConfig)
ScopeOption defines a functional option for Scope configuration
func EnabledBy ¶
func EnabledBy(envVars ...string) ScopeOption
EnabledBy creates a ScopeOption that enables scope activation via environment variables.
Multiple environment variables behavior:
- If ANY of the specified environment variables is set (even to empty string), the scope will be activated.
- Environment variables are checked with os.LookupEnv(), so existence matters, not value.
Example:
scope := ctxlog.NewScope("api", ctxlog.EnabledBy("DEBUG_API", "VERBOSE_API"))
// Scope is active if either DEBUG_API OR VERBOSE_API is set
export DEBUG_API=1 # scope is active
export VERBOSE_API="" # scope is active (empty value still counts)
unset DEBUG_API VERBOSE_API # scope is inactive