Documentation
¶
Index ¶
- Constants
- Variables
- func Dump(key string, v any, opts ...DumpOption) slog.Attr
- func DumpGroup(key string, v any, opts ...DumpOption) slog.Attr
- func DumpGroupWith(l *Logger, key string, v any, opts ...DumpOption) slog.Attr
- func DumpWith(l *Logger, key string, v any, opts ...DumpOption) slog.Attr
- func Err(err error) slog.Attr
- func GRPCCall(name, method string, req any, md Metadata, opts ...DumpOption) slog.Attr
- func GRPCCallWith(l *Logger, name, method string, req any, md Metadata, opts ...DumpOption) slog.Attr
- func GRPCResponse(name string, resp any, opts ...DumpOption) slog.Attr
- func GRPCResponseWith(l *Logger, name string, resp any, opts ...DumpOption) slog.Attr
- func GRPCStreamEvent(name, method, event string, msg any, md Metadata, opts ...DumpOption) slog.Attr
- func GRPCStreamEventWith(l *Logger, name, method, event string, msg any, md Metadata, ...) slog.Attr
- func HTTPRequest(name string, r *http.Request, opts ...DumpOption) slog.Attr
- func HTTPRequestWith(l *Logger, name string, r *http.Request, opts ...DumpOption) slog.Attr
- func HTTPResponse(name string, r *http.Response, opts ...DumpOption) slog.Attr
- func HTTPResponseWith(l *Logger, name string, r *http.Response, opts ...DumpOption) slog.Attr
- func RestoreBody(r *http.Request, body string)
- func Serialize(cfg Config, v any, opts ...DumpOption) any
- func SerializeDefault(v any, opts ...DumpOption) any
- func SetGlobalDumpConfig(cfg *Config)
- func ToContext(ctx context.Context, l *Logger) context.Context
- func Value(ctx context.Context, key ContextKey) any
- func WithValue(ctx context.Context, key ContextKey, val any) context.Context
- type Config
- type ContextKey
- type DefaultMasker
- type DumpOption
- type DynamicHandler
- type Format
- type LevelNames
- type Logger
- func (l *Logger) Config() Config
- func (l *Logger) FatalContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) SetLevel(lvl slog.Level)
- func (l *Logger) TraceContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) UpdateConfig(fn func(*Config))
- func (l *Logger) With(args ...any) *Logger
- func (l *Logger) WithGroup(name string) *Logger
- type MaskMap
- type MaskRules
- type MaskType
- type Masker
- type Metadata
- type Option
- func WithAddSource(enabled bool) Option
- func WithContextKeys(keys ...ContextKey) Option
- func WithDumpLimits(maxDepth, maxFields int) Option
- func WithExitFunc(fn func(int)) Option
- func WithFormat(f Format) Option
- func WithLevel(l slog.Level) Option
- func WithLevelNames(m LevelNames) Option
- func WithMaskKey(key string, mType MaskType) Option
- func WithMaskKeys(keys MaskMap) Option
- func WithMaskRules(r *MaskRules) Option
- func WithMasker(m Masker) Option
- func WithOutput(w io.Writer) Option
- func WithRemoval(set *RemovalSet) Option
- func WithStackDepth(depth int) Option
- func WithStackOnError(enabled bool) Option
- func WithTraceContext(enabled bool) Option
- func WithTraceKeys(traceID, spanID, traceSampled string) Option
- type RemovalSet
- type RemoveMap
Constants ¶
const ( // LevelTrace is below slog.LevelDebug for verbose diagnostics. LevelTrace = slog.Level(-8) // LevelFatal is above slog.LevelError for unrecoverable failures. LevelFatal = slog.Level(12) )
Variables ¶
var ( KeyRequestID = ContextKey("request_id") KeyUserID = ContextKey("user_id") KeyTenantID = ContextKey("tenant_id") )
Standard context keys for enterprise services.
Functions ¶
func Dump ¶
func Dump(key string, v any, opts ...DumpOption) slog.Attr
Dump serializes v into a slog attribute using saferefl with cycle detection.
func DumpGroup ¶
func DumpGroup(key string, v any, opts ...DumpOption) slog.Attr
DumpGroup wraps a serialized value as a slog group attribute.
func DumpGroupWith ¶
DumpGroupWith serializes v as a group using configuration from l when available.
func GRPCCallWith ¶
func GRPCCallWith(l *Logger, name, method string, req any, md Metadata, opts ...DumpOption) slog.Attr
GRPCCallWith logs a sanitized gRPC request using logger dump settings.
func GRPCResponse ¶
func GRPCResponse(name string, resp any, opts ...DumpOption) slog.Attr
GRPCResponse logs a sanitized gRPC response snapshot.
func GRPCResponseWith ¶
GRPCResponseWith logs a sanitized gRPC response using logger dump settings.
func GRPCStreamEvent ¶
func GRPCStreamEvent(name, method, event string, msg any, md Metadata, opts ...DumpOption) slog.Attr
GRPCStreamEvent logs a streaming gRPC event with optional metadata.
func GRPCStreamEventWith ¶
func GRPCStreamEventWith(l *Logger, name, method, event string, msg any, md Metadata, opts ...DumpOption) slog.Attr
GRPCStreamEventWith logs a streaming gRPC event using logger dump settings.
func HTTPRequest ¶
HTTPRequest logs a sanitized HTTP request snapshot.
func HTTPRequestWith ¶
HTTPRequestWith logs a sanitized HTTP request using logger dump settings.
func HTTPResponse ¶
HTTPResponse logs a sanitized HTTP response snapshot.
func HTTPResponseWith ¶
HTTPResponseWith logs a sanitized HTTP response using logger dump settings.
func RestoreBody ¶
RestoreBody replaces r.Body after logging so downstream handlers can still read it.
func Serialize ¶
func Serialize(cfg Config, v any, opts ...DumpOption) any
Serialize converts v to a log-safe representation using saferefl.
func SerializeDefault ¶
func SerializeDefault(v any, opts ...DumpOption) any
SerializeDefault uses Default dump limits and masking rules.
func SetGlobalDumpConfig ¶
func SetGlobalDumpConfig(cfg *Config)
SetGlobalDumpConfig sets defaults used by Dump when no Logger is available.
Types ¶
type Config ¶
type Config struct {
Level slog.Level
Format Format
Output io.Writer
MaskKeys MaskMap
RemoveKeys RemoveMap
LevelNames LevelNames
Masker Masker
ContextKeys []ContextKey
AddSource bool
StackOnError bool
StackDepth int
TraceContext bool
TraceIDKey string
SpanIDKey string
TraceSampleKey string
DumpMaxDepth int
DumpMaxFields int
}
Config holds the atomic logger configuration state.
type ContextKey ¶
type ContextKey string
ContextKey is a typed key for values stored in context.Context.
func (ContextKey) String ¶
func (k ContextKey) String() string
String returns the key name used in log attributes.
type DefaultMasker ¶
type DefaultMasker struct{}
DefaultMasker provides built-in redaction for common sensitive data types.
type DumpOption ¶
type DumpOption func(*dumpOptions)
DumpOption configures Dump and payload helpers.
func WithDumpDepth ¶
func WithDumpDepth(depth int) DumpOption
WithDumpDepth overrides the maximum recursion depth.
func WithDumpMaskKeys ¶
func WithDumpMaskKeys(keys MaskMap) DumpOption
WithDumpMaskKeys applies masking rules during dump serialization.
func WithDumpMaxFields ¶
func WithDumpMaxFields(n int) DumpOption
WithDumpMaxFields overrides the maximum number of fields emitted.
func WithDumpRemoval ¶
func WithDumpRemoval(keys ...string) DumpOption
WithDumpRemoval excludes keys during dump serialization.
type DynamicHandler ¶
type DynamicHandler struct {
// contains filtered or unexported fields
}
DynamicHandler is a slog.Handler with runtime reconfiguration, OTel context injection, and stack traces on errors.
type Format ¶
type Format int
Format defines the output format for the logger.
func ParseFormat ¶
ParseFormat converts a string to Format. Defaults to FormatText.
type LevelNames ¶
LevelNames maps slog.Level values to custom string labels in log output.
type Logger ¶
Logger wraps slog.Logger with atomic runtime configuration updates.
func Default ¶
func Default() *Logger
Default returns a production-ready logger: JSON, Info level, OTel, stack on error.
func FromContext ¶
FromContext extracts a Logger from context or returns a wrapper around slog.Default().
func MustDefault ¶
func MustDefault() *Logger
MustDefault sets Default() as slog.Default() and returns it.
func SetupDefault ¶
SetupDefault initializes a Logger and sets it as slog.Default().
func (*Logger) FatalContext ¶
FatalContext logs at LevelFatal and exits the process.
func (*Logger) TraceContext ¶
TraceContext logs at LevelTrace.
func (*Logger) UpdateConfig ¶
UpdateConfig atomically updates logger configuration using copy-on-write.
type MaskRules ¶
type MaskRules struct {
// contains filtered or unexported fields
}
MaskRules is a fluent builder for masking configuration.
type MaskType ¶
type MaskType int
MaskType defines the category of data being masked.
const ( // MaskDefault replaces the value with [MASKED]. MaskDefault MaskType = iota // MaskEmail redacts email addresses while preserving parts for debugging. MaskEmail // MaskPhone redacts phone numbers while preserving prefix and suffix. MaskPhone // MaskCard redacts credit card numbers. MaskCard // MaskSecret completely hides the value as [SECRET]. MaskSecret )
type Option ¶
type Option func(*options)
Option configures logger initialization.
func WithAddSource ¶
WithAddSource enables source file and line in log records.
func WithContextKeys ¶
func WithContextKeys(keys ...ContextKey) Option
WithContextKeys registers typed keys extracted from context.Context.
func WithDumpLimits ¶
WithDumpLimits sets default depth and field limits for Dump and payload helpers.
func WithExitFunc ¶
WithExitFunc overrides the process exit function used by Fatal.
func WithLevelNames ¶
func WithLevelNames(m LevelNames) Option
WithLevelNames customizes level string labels.
func WithMaskKey ¶
WithMaskKey associates a single attribute key with a MaskType.
func WithMaskKeys ¶
WithMaskKeys applies a batch of masking rules.
func WithMaskRules ¶
WithMaskRules applies masking rules from a MaskRules builder.
func WithRemoval ¶
func WithRemoval(set *RemovalSet) Option
WithRemoval registers keys from a RemovalSet for removal.
func WithStackDepth ¶
WithStackDepth sets how many stack frames to capture on errors.
func WithStackOnError ¶
WithStackOnError attaches a stack trace to Error and Fatal records.
func WithTraceContext ¶
WithTraceContext enables OpenTelemetry trace_id and span_id extraction.
func WithTraceKeys ¶
WithTraceKeys customizes OTel field names in log output.
type RemovalSet ¶
type RemovalSet struct {
// contains filtered or unexported fields
}
RemovalSet collects attribute keys that must be stripped from log output.
func NewRemovalSet ¶
func NewRemovalSet(keys ...string) *RemovalSet
NewRemovalSet creates an empty RemovalSet or initializes it with keys.
func (*RemovalSet) Add ¶
func (s *RemovalSet) Add(keys ...string) *RemovalSet
Add appends one or more keys to the removal set.
func (*RemovalSet) Keys ¶
func (s *RemovalSet) Keys() []string
Keys returns the underlying slice of keys.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
Package main demonstrates basic slogsafe setup: levels, formats, and helpers.
|
Package main demonstrates basic slogsafe setup: levels, formats, and helpers. |
|
context
command
Package main demonstrates typed context keys and logger injection.
|
Package main demonstrates typed context keys and logger injection. |
|
dump
command
Package main demonstrates Dump / Serialize for nested structs via saferefl.
|
Package main demonstrates Dump / Serialize for nested structs via saferefl. |
|
grpc_payload
command
Package main demonstrates sanitized gRPC call / response / stream logging.
|
Package main demonstrates sanitized gRPC call / response / stream logging. |
|
http_payload
command
Package main demonstrates sanitized HTTP request/response payload logging.
|
Package main demonstrates sanitized HTTP request/response payload logging. |
|
masking
command
Package main demonstrates data masking and field removal.
|
Package main demonstrates data masking and field removal. |
|
presets
command
Package main demonstrates Default / Dev / MustDefault presets.
|
Package main demonstrates Default / Dev / MustDefault presets. |
|
runtime_config
command
Package main demonstrates runtime config updates (level / format) without restart.
|
Package main demonstrates runtime config updates (level / format) without restart. |
|
stack
command
Package main demonstrates stack traces and source location on Error/Fatal.
|
Package main demonstrates stack traces and source location on Error/Fatal. |
|
tracing
command
Package main demonstrates OpenTelemetry trace_id / span_id injection.
|
Package main demonstrates OpenTelemetry trace_id / span_id injection. |