Documentation
¶
Index ¶
- func FilterFields(entry map[string]any, includeFields, excludeFields []string) map[string]any
- type DebugLogger
- type FieldMap
- type Format
- type HandlerOptions
- type Level
- type LogfmtHandler
- type LogstashHandler
- type MultiWriter
- type NoOpLogger
- func (n *NoOpLogger) Close() error
- func (n *NoOpLogger) Enabled() bool
- func (n *NoOpLogger) LogDebug(ctx context.Context, message string, fieldMap ...FieldMap)
- func (n *NoOpLogger) LogError(_ context.Context, _ string, _ error, _ ...FieldMap)
- func (n *NoOpLogger) LogInfo(ctx context.Context, message string, fieldMap ...FieldMap)
- func (n *NoOpLogger) LogWarn(ctx context.Context, message string, fieldMap ...FieldMap)
- func (n *NoOpLogger) WithFields(_ FieldMap) DebugLogger
- func (n *NoOpLogger) WithGroup(_ string) DebugLogger
- type Option
- func WithExcludeFields(fields []string) Option
- func WithFields(fields FieldMap) Option
- func WithGroup(name string) Option
- func WithIncludeFields(fields []string) Option
- func WithMinLevel(level Level) Option
- func WithOutputFile(path string) Option
- func WithSyncInterval(interval time.Duration) Option
- func WithSyncMode(mode SyncMode) Option
- type SlogLogger
- func (s *SlogLogger) Close() error
- func (s *SlogLogger) Enabled() bool
- func (s *SlogLogger) LogDebug(ctx context.Context, message string, fieldMap ...FieldMap)
- func (s *SlogLogger) LogError(ctx context.Context, message string, err error, fieldMap ...FieldMap)
- func (s *SlogLogger) LogInfo(ctx context.Context, message string, fieldMap ...FieldMap)
- func (s *SlogLogger) LogWarn(ctx context.Context, message string, fieldMap ...FieldMap)
- func (s *SlogLogger) WithFields(fields FieldMap) DebugLogger
- func (s *SlogLogger) WithGroup(name string) DebugLogger
- type SyncMode
- type TextHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DebugLogger ¶
type DebugLogger interface {
// Enabled returns true if debug logging is enabled.
// When false, the logger will skip logging entirely for efficiency.
Enabled() bool
// LogDebug logs string message with slog.Level = LevelDebug.
LogDebug(ctx context.Context, message string, fieldMap ...FieldMap)
// LogInfo logs string message with slog.Level = LevelInfo.
LogInfo(ctx context.Context, message string, fieldMap ...FieldMap)
// LogWarn logs string message with slog.Level = LevelWarn.
LogWarn(ctx context.Context, message string, fieldMap ...FieldMap)
// LogError logs string message with slog.level = LevelError.
LogError(ctx context.Context, message string, err error, fieldMap ...FieldMap)
// WithFields returns a logger with pre-populated fields.
WithFields(fields FieldMap) DebugLogger
// WithGroup returns a logger with all subsequent attributes grouped under the given name.
WithGroup(name string) DebugLogger
// Close flushes any buffered output and closes file handles.
Close() error
}
DebugLogger provides structured debug logging capabilities. All methods are no-ops when debug mode is disabled.
func NewSlogLogger ¶
func NewSlogLogger(enabled bool, format Format, opts ...Option) (DebugLogger, error)
NewSlogLogger creates a new SlogLogger with the given configuration. If enabled is false, a NoOpLogger is returned instead. Optional parameters can be provided using WithMinLevel, WithIncludeFields, WithExcludeFields, and WithOutputFile.
type Format ¶
type Format string
Format represents the output format for debug logging.
const ( // FormatJSON outputs logs in standard JSON format using slog.JSONHandler. // Fields: time, level, msg with nested group objects. FormatJSON Format = "json" // FormatText outputs logs in human-readable text format. FormatText Format = "text" // FormatLogfmt outputs logs in logfmt format (key=value pairs). FormatLogfmt Format = "logfmt" // FormatLogstash outputs logs in Logstash/Elasticsearch compatible JSON format. // Fields: @timestamp, log.level, message with flattened group keys (e.g., request.id). FormatLogstash Format = "logstash" )
type HandlerOptions ¶
type HandlerOptions struct {
// Level is the minimum log level to output.
Level Level
// IncludeFields filters fields to include (empty = all).
// When specified, only fields in this list will be included in the output.
// Built-in fields like @timestamp, @version, level, message, thread_name
// should be included explicitly if you want them in the output.
IncludeFields []string
// ExcludeFields filters fields to exclude from the output.
// Useful for removing sensitive data like passwords or API keys.
ExcludeFields []string
}
HandlerOptions configures handler behavior for all output formats. It provides a unified configuration interface for TextHandler, LogfmtHandler, and LogstashHandler.
type Level ¶ added in v1.0.1
type Level int
Level represents a log level. It wraps slog.Level to provide a cleaner API without exposing slog directly.
type LogfmtHandler ¶
type LogfmtHandler struct {
// contains filtered or unexported fields
}
LogfmtHandler creates logfmt-formatted output. Logfmt is a key=value format that is both human-readable and machine-parseable.
Implements slog.Handler interface.
func NewLogfmtHandler ¶
func NewLogfmtHandler(w io.Writer, opts *HandlerOptions) *LogfmtHandler
NewLogfmtHandler creates a new LogfmtHandler writing to the specified writer.
type LogstashHandler ¶
type LogstashHandler struct {
// contains filtered or unexported fields
}
LogstashHandler is a custom slog.Handler that outputs logs in Logstash/Elasticsearch compatible JSON format. It wraps slog.JSONHandler for optimal performance.
Key differences from standard JSONHandler:
- Field names: @timestamp, log.level, message (instead of time, level, msg)
- Groups are flattened with dot notation (e.g., request.id instead of request: {id: ...})
- Supports field filtering via include/exclude lists
Implements slog.Handler interface.
func NewLogstashHandler ¶
func NewLogstashHandler(w io.Writer, opts *HandlerOptions) *LogstashHandler
NewLogstashHandler creates a new LogstashHandler writing to the specified writer.
func (*LogstashHandler) Enabled ¶
Enabled returns true if the handler should log at the given level.
func (*LogstashHandler) Handle ¶
Handle processes the log record and writes it in Logstash JSON format. It flattens groups and renames standard fields before delegating to JSONHandler.
type MultiWriter ¶
type MultiWriter struct {
// contains filtered or unexported fields
}
MultiWriter writes to multiple outputs simultaneously. It supports writing to stdout and/or a file.
func NewMultiWriter ¶
func NewMultiWriter(outputFile string, syncMode SyncMode, syncInterval time.Duration) (*MultiWriter, error)
NewMultiWriter creates a writer that outputs to stdout and/or file. If outputFile is empty, only stdout is used.
func (*MultiWriter) Close ¶
func (m *MultiWriter) Close() error
Close closes the file handle if one was opened.
type NoOpLogger ¶
type NoOpLogger struct{}
NoOpLogger is a no-operation implementation of DebugLogger. It provides zero overhead when debug mode is disabled. All methods are empty and Enabled() always returns false.
func NewNoOpLogger ¶
func NewNoOpLogger() *NoOpLogger
NewNoOpLogger creates a new NoOpLogger instance.
func (*NoOpLogger) Close ¶
func (n *NoOpLogger) Close() error
Close returns nil - no resources to release.
func (*NoOpLogger) Enabled ¶
func (n *NoOpLogger) Enabled() bool
Enabled returns false - debug logging is disabled.
func (*NoOpLogger) LogDebug ¶
func (n *NoOpLogger) LogDebug(ctx context.Context, message string, fieldMap ...FieldMap)
LogDebug logs string message with slog.Level = LevelDebug.
func (*NoOpLogger) LogInfo ¶
func (n *NoOpLogger) LogInfo(ctx context.Context, message string, fieldMap ...FieldMap)
LogDebug logs string message with slog.Level = LevelInfo.
func (*NoOpLogger) LogWarn ¶
func (n *NoOpLogger) LogWarn(ctx context.Context, message string, fieldMap ...FieldMap)
LogDebug logs string message with slog.Level = LevelWarn.
func (*NoOpLogger) WithFields ¶
func (n *NoOpLogger) WithFields(_ FieldMap) DebugLogger
WithFields returns the same NoOpLogger instance.
func (*NoOpLogger) WithGroup ¶
func (n *NoOpLogger) WithGroup(_ string) DebugLogger
WithGroup returns the same NoOpLogger instance.
type Option ¶
type Option func(*config)
Option is a functional option for configuring the logger.
func WithExcludeFields ¶
WithExcludeFields sets the fields to exclude from log output.
func WithFields ¶
WithFields sets pre-populated fields for all log entries. These fields will be included in every log message from this logger.
func WithGroup ¶
WithGroup sets a group name for all subsequent attributes. All fields will be prefixed with the group name (e.g., "myservice.field").
func WithIncludeFields ¶
WithIncludeFields sets the fields to include in log output. If specified, only these fields will be included.
func WithOutputFile ¶ added in v1.0.1
WithOutputFile enables file output in addition to stdout. If not called, logs are written to stdout only.
func WithSyncInterval ¶
WithSyncInterval sets the flush interval for SyncPeriodic mode. Only used when SyncMode is SyncPeriodic. Default is 1 second.
func WithSyncMode ¶
WithSyncMode sets the sync mode for file output. Default is SyncImmediate for maximum durability.
type SlogLogger ¶
type SlogLogger struct {
// contains filtered or unexported fields
}
SlogLogger wraps slog.Logger and implements the DebugLogger interface. It provides structured logging with domain-specific methods for pipeline stages, retry attempts, and rate limiting decisions.
func (*SlogLogger) Close ¶
func (s *SlogLogger) Close() error
Close flushes any buffered output and closes file handles.
func (*SlogLogger) Enabled ¶
func (s *SlogLogger) Enabled() bool
Enabled returns true if debug logging is enabled.
func (*SlogLogger) LogDebug ¶
func (s *SlogLogger) LogDebug(ctx context.Context, message string, fieldMap ...FieldMap)
LogDebug logs a debug-level message with context. Pre-populated fields from WithFields are already baked into the logger.
func (*SlogLogger) LogError ¶
LogError logs an error-level message with context. Pre-populated fields from WithFields are already baked into the logger.
func (*SlogLogger) LogInfo ¶
func (s *SlogLogger) LogInfo(ctx context.Context, message string, fieldMap ...FieldMap)
LogInfo logs an info-level message with context. Pre-populated fields from WithFields are already baked into the logger.
func (*SlogLogger) LogWarn ¶
func (s *SlogLogger) LogWarn(ctx context.Context, message string, fieldMap ...FieldMap)
LogWarn logs a warn-level message with context. Pre-populated fields from WithFields are already baked into the logger.
func (*SlogLogger) WithFields ¶
func (s *SlogLogger) WithFields(fields FieldMap) DebugLogger
WithFields returns a logger with pre-populated fields. Fields are baked into the slog.Logger for optimal performance.
func (*SlogLogger) WithGroup ¶
func (s *SlogLogger) WithGroup(name string) DebugLogger
WithGroup returns a logger with all subsequent attributes grouped under the given name.
type SyncMode ¶
type SyncMode int
SyncMode determines when file writes are flushed to disk.
const ( // SyncImmediate flushes after every write (maximum durability, slower). SyncImmediate SyncMode = iota // SyncBuffered buffers writes and flushes only on Close() (better performance). SyncBuffered // SyncPeriodic flushes at regular intervals (balance of durability/performance). SyncPeriodic )
type TextHandler ¶
type TextHandler struct {
// contains filtered or unexported fields
}
TextHandler creates a human-readable text output.
Implements slog.Handler interface.
func NewTextHandler ¶
func NewTextHandler(w io.Writer, opts *HandlerOptions) *TextHandler
NewTextHandler creates a new TextHandler writing to the specified writer.
func (*TextHandler) Handle ¶
Handle processes the log record and writes it in human-readable text format.