Documentation
¶
Overview ¶
Package logging provides structured logging with request tracing and sensitive data redaction.
Index ¶
- Constants
- func GenerateRequestID() string
- func GenerateSpanID() string
- func GetRequestID(ctx context.Context) string
- func GetSpanID(ctx context.Context) string
- func GetTraceID(ctx context.Context) string
- func GetUserID(ctx context.Context) string
- func InjectRequestID(next http.Handler) http.Handler
- func IsSensitiveField(field string) bool
- func LoggerFromContext(ctx context.Context, baseLogger *slog.Logger) *slog.Logger
- func ModuleLogger(module string) *slog.Logger
- func ParseLevel(level string) slog.Level
- func RedactSensitive(data map[string]any) map[string]any
- func RedactStringValue(s string) string
- func RequestLogger(logger *slog.Logger) func(http.Handler) http.Handler
- func SafeAttrs(data map[string]any) []slog.Attr
- func WithRequestID(ctx context.Context, id string) context.Context
- func WithSpanID(ctx context.Context, id string) context.Context
- func WithTraceID(ctx context.Context, id string) context.Context
- func WithUserID(ctx context.Context, id string) context.Context
- type Config
- type ContextHandler
- type HTTPMiddleware
- type Logger
- func (l *Logger) SetDefault()
- func (l *Logger) With(args ...any) *Logger
- func (l *Logger) WithEntity(entity, id string) *Logger
- func (l *Logger) WithGroup(name string) *Logger
- func (l *Logger) WithJob(jobID string) *Logger
- func (l *Logger) WithModule(module string) *Logger
- func (l *Logger) WithOperation(operation string) *Logger
- func (l *Logger) WithWorkflow(workflowID, executionID string) *Logger
- type RedactingHandler
- type Redactor
- func (r *Redactor) AddAllowlistField(field string)
- func (r *Redactor) AddSensitiveField(field string)
- func (r *Redactor) AddSensitivePattern(pattern string) error
- func (r *Redactor) IsSensitiveField(field string) bool
- func (r *Redactor) RedactMap(data map[string]any) map[string]any
- func (r *Redactor) RedactString(s string) string
- func (r *Redactor) SafeAttrs(data map[string]any) []slog.Attr
- type TraceContext
- func (tc TraceContext) ToContext(ctx context.Context) context.Context
- func (tc TraceContext) WithRequestID(id string) TraceContext
- func (tc TraceContext) WithSpanID(id string) TraceContext
- func (tc TraceContext) WithTraceID(id string) TraceContext
- func (tc TraceContext) WithUserID(id string) TraceContext
- type Verbosity
Constants ¶
const ( // RequestIDKey is the context key for request IDs. RequestIDKey contextKey = "request_id" // TraceIDKey is the context key for trace IDs. TraceIDKey contextKey = "trace_id" // UserIDKey is the context key for user IDs. UserIDKey contextKey = "user_id" // SpanIDKey is the context key for span IDs. SpanIDKey contextKey = "span_id" )
const RedactedValue = "[REDACTED]"
const RequestHeaderRequestID = "X-Request-ID"
RequestHeaderRequestID is the header name for request ID.
const RequestHeaderTraceID = "X-Trace-ID"
RequestHeaderTraceID is the header name for trace ID.
Variables ¶
This section is empty.
Functions ¶
func GenerateRequestID ¶
func GenerateRequestID() string
GenerateRequestID generates a new UUID v4 request ID.
func GetRequestID ¶
GetRequestID retrieves the request ID from the context.
func GetTraceID ¶
GetTraceID retrieves the trace ID from the context.
func InjectRequestID ¶
InjectRequestID is a simple middleware that only injects request ID into context.
func IsSensitiveField ¶
IsSensitiveField checks if a field name is sensitive using the default redactor.
func LoggerFromContext ¶
LoggerFromContext returns a logger with context values pre-populated.
func ModuleLogger ¶
ModuleLogger creates a logger for a specific module using the default logger.
func ParseLevel ¶
ParseLevel converts a string level to slog.Level.
func RedactSensitive ¶
RedactSensitive redacts sensitive fields from a map using the default redactor.
func RedactStringValue ¶
RedactString redacts sensitive patterns from a string using the default redactor.
func RequestLogger ¶
RequestLogger is a convenience function to create a middleware that logs requests.
func SafeAttrs ¶
SafeAttrs creates slog attributes with sensitive data redacted using the default redactor.
func WithRequestID ¶
WithRequestID adds a request ID to the context.
func WithSpanID ¶
WithSpanID adds a span ID to the context.
func WithTraceID ¶
WithTraceID adds a trace ID to the context.
Types ¶
type Config ¶
type Config struct {
// Level sets the minimum log level: debug, info, warn, error
Level string
// Format specifies the output format: json or text
Format string
// Output specifies the output destination: stdout, stderr, or a file path
Output string
// AddSource adds source file and line number to log entries
AddSource bool
// SampleRate for high-volume logs (0.0-1.0, 1.0 = log all)
SampleRate float64
// SlowQueryThreshold marks queries slower than this as slow
SlowQueryThreshold time.Duration
// RedactPatterns are additional patterns to redact
RedactPatterns []string
// AllowlistFields are field names that should not be redacted even if they match patterns
AllowlistFields []string
}
Config holds the logging configuration.
func ConfigFromEnv ¶
func ConfigFromEnv() Config
ConfigFromEnv creates a configuration from environment variables.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns sensible defaults for logging configuration.
type ContextHandler ¶
ContextHandler is a slog.Handler that extracts context values.
func (*ContextHandler) Enabled ¶
Enabled reports whether the handler handles records at the given level.
func (*ContextHandler) Handle ¶
Handle adds context values to the log record and passes to the wrapped handler.
type HTTPMiddleware ¶
type HTTPMiddleware struct {
// contains filtered or unexported fields
}
HTTPMiddleware provides HTTP request logging middleware.
func NewHTTPMiddleware ¶
func NewHTTPMiddleware(logger *slog.Logger) *HTTPMiddleware
NewHTTPMiddleware creates a new HTTP logging middleware.
func (*HTTPMiddleware) Handler ¶
func (m *HTTPMiddleware) Handler(next http.Handler) http.Handler
Handler returns the middleware handler.
func (*HTTPMiddleware) WithVerbosity ¶
func (m *HTTPMiddleware) WithVerbosity(v Verbosity) *HTTPMiddleware
WithVerbosity sets the logging verbosity level.
type Logger ¶
Logger wraps slog.Logger with additional functionality.
func Default ¶
func Default() *Logger
Default returns a default logger using environment configuration.
func NewWithWriter ¶
NewWithWriter creates a new Logger with a custom writer.
func (*Logger) SetDefault ¶
func (l *Logger) SetDefault()
SetDefault sets this logger as the default slog logger.
func (*Logger) WithEntity ¶
WithEntity returns a new Logger with entity context.
func (*Logger) WithModule ¶
WithModule returns a new Logger with module context.
func (*Logger) WithOperation ¶
WithOperation returns a new Logger with operation context.
func (*Logger) WithWorkflow ¶
WithWorkflow returns a new Logger with workflow context.
type RedactingHandler ¶
RedactingHandler wraps a slog.Handler to redact sensitive data from log records.
func NewRedactingHandler ¶
func NewRedactingHandler(handler slog.Handler, redactor *Redactor) *RedactingHandler
NewRedactingHandler creates a new RedactingHandler.
type Redactor ¶
type Redactor struct {
// contains filtered or unexported fields
}
Redactor handles redaction of sensitive data.
func NewRedactor ¶
func NewRedactor() *Redactor
NewRedactor creates a new Redactor with default settings.
func (*Redactor) AddAllowlistField ¶
AddAllowlistField adds a field to the allowlist (won't be redacted even if matching).
func (*Redactor) AddSensitiveField ¶
AddSensitiveField adds a field name to the sensitive list.
func (*Redactor) AddSensitivePattern ¶
AddSensitivePattern adds a regex pattern to detect sensitive data.
func (*Redactor) IsSensitiveField ¶
IsSensitiveField checks if a field name is sensitive.
func (*Redactor) RedactString ¶
RedactString redacts sensitive patterns from a string.
type TraceContext ¶
TraceContext holds tracing information for a request.
func FromContext ¶
func FromContext(ctx context.Context) TraceContext
FromContext extracts a TraceContext from a context.Context.
func NewTraceContext ¶
func NewTraceContext() TraceContext
NewTraceContext creates a new TraceContext with generated IDs.
func NewTraceContextWithParent ¶
func NewTraceContextWithParent(parentTraceID string) TraceContext
NewTraceContextWithParent creates a new TraceContext inheriting the parent trace ID.
func (TraceContext) ToContext ¶
func (tc TraceContext) ToContext(ctx context.Context) context.Context
ToContext adds the trace context to a context.Context.
func (TraceContext) WithRequestID ¶
func (tc TraceContext) WithRequestID(id string) TraceContext
WithRequestID sets the request ID.
func (TraceContext) WithSpanID ¶
func (tc TraceContext) WithSpanID(id string) TraceContext
WithSpanID sets the span ID.
func (TraceContext) WithTraceID ¶
func (tc TraceContext) WithTraceID(id string) TraceContext
WithTraceID sets the trace ID.
func (TraceContext) WithUserID ¶
func (tc TraceContext) WithUserID(id string) TraceContext
WithUserID sets the user ID.