Documentation
¶
Overview ¶
File: default.go
File: l/fast_middleware.go
Package l provides a structured logging framework with interface-based components ¶
l/middleware.go
l/new_logger.go
Index ¶
- Constants
- Variables
- func Debug(msg string, args ...any)
- func Error(msg string, args ...any)
- func FastLoggingMiddleware(next fasthttp.RequestHandler) fasthttp.RequestHandler
- func Info(msg string, args ...any)
- func SetDefaultLogger(logger Logger)
- func Warn(msg string, args ...any)
- type BatchProcessor
- type BufferManager
- type BufferedWriter
- type Config
- type Configurable
- type ContextProvider
- type DirectWriter
- type Enricher
- type ErrorHandler
- type Factory
- type FieldProvider
- type FileWriter
- type FilterFunc
- type Filterable
- type Flushable
- type Formatter
- type FormatterOptions
- type HandlerWrapper
- type HealthChecker
- type JSONFormatter
- type Level
- type Lifecycle
- type LogRecord
- type Logger
- type Metrics
- type MetricsCollector
- type QueryOptions
- type Queryable
- type RateLimiter
- type Rotatable
- type RotationManager
- type Sampler
- type StackTraceLine
- type StandardBufferManager
- type StandardErrorHandler
- type StandardFactory
- func (f *StandardFactory) CreateBufferManager(config Config) (BufferManager, error)
- func (f *StandardFactory) CreateErrorHandler(config Config) (ErrorHandler, error)
- func (f *StandardFactory) CreateFormatter(config Config) (Formatter, error)
- func (f *StandardFactory) CreateHandlerWrapper(handler slog.Handler, closer io.Closer) HandlerWrapper
- func (f *StandardFactory) CreateHealthChecker(logger Logger) (HealthChecker, error)
- func (f *StandardFactory) CreateLogger(config Config) (Logger, error)
- func (f *StandardFactory) CreateMetricsCollector(config Config) (MetricsCollector, error)
- func (f *StandardFactory) CreateRotationManager(config Config) (RotationManager, error)
- func (f *StandardFactory) CreateWriter(config Config) (Writer, error)
- type StandardHandlerWrapper
- func (h *StandardHandlerWrapper) Close() error
- func (h *StandardHandlerWrapper) Enabled(ctx context.Context, level slog.Level) bool
- func (h *StandardHandlerWrapper) Handle(ctx context.Context, r slog.Record) error
- func (h *StandardHandlerWrapper) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *StandardHandlerWrapper) WithGroup(name string) slog.Handler
- type StandardHealthChecker
- type StandardLogger
- func (l *StandardLogger) Close() error
- func (l *StandardLogger) Debug(msg string, args ...any)
- func (l *StandardLogger) Error(msg string, args ...any)
- func (l *StandardLogger) Flush() error
- func (l *StandardLogger) GetContext() context.Context
- func (l *StandardLogger) GetFields() map[string]interface{}
- func (l *StandardLogger) Info(msg string, args ...any)
- func (l *StandardLogger) LogError(ctx context.Context, err error)
- func (l *StandardLogger) RecoveryMiddleware() func(http.Handler) http.Handler
- func (l *StandardLogger) RequestLogger() func(http.Handler) http.Handler
- func (l *StandardLogger) Warn(msg string, args ...any)
- func (l *StandardLogger) With(args ...any) Logger
- func (l *StandardLogger) WithContext(ctx context.Context) Logger
- func (l *StandardLogger) WithError(err error) Logger
- func (l *StandardLogger) WithFields(fields map[string]interface{}) Logger
- type StandardMetricsCollector
- func (m *StandardMetricsCollector) GetMetrics() Metrics
- func (m *StandardMetricsCollector) IncrementDropped()
- func (m *StandardMetricsCollector) IncrementErrors()
- func (m *StandardMetricsCollector) IncrementTotal()
- func (m *StandardMetricsCollector) Reset()
- func (m *StandardMetricsCollector) SetLastError(t time.Time)
- func (m *StandardMetricsCollector) SetLastFlush(t time.Time)
- type StandardRotationManager
- type TextFormatter
- type Validator
- type Writer
Constants ¶
const ( LevelDebug = slog.LevelDebug LevelInfo = slog.LevelInfo LevelWarn = slog.LevelWarn LevelError = slog.LevelError )
Predefined log levels.
Variables ¶
var ErrBufferFull = fmt.Errorf("log buffer full")
Functions ¶
func FastLoggingMiddleware ¶ added in v1.5.1
func FastLoggingMiddleware(next fasthttp.RequestHandler) fasthttp.RequestHandler
FastLoggingMiddleware returns a fasthttp.RequestHandler middleware that logs incoming requests and their durations using the default logger. When debug-level logging is enabled, it logs additional details such as request headers, query parameters, cookies, and a snippet of the request body, as well as response headers.
func SetDefaultLogger ¶ added in v1.2.0
func SetDefaultLogger(logger Logger)
SetDefaultLogger allows users to replace the default logger with their own instance
Types ¶
type BatchProcessor ¶ added in v1.2.0
BatchProcessor defines batch processing capabilities
type BufferManager ¶ added in v1.2.0
BufferManager handles message buffering operations
type BufferedWriter ¶ added in v1.2.0
type BufferedWriter struct {
// contains filtered or unexported fields
}
BufferedWriter implements Writer with async buffering
func NewBufferedWriter ¶ added in v1.2.0
func NewBufferedWriter(out io.Writer, bufferSize int) *BufferedWriter
func (*BufferedWriter) Close ¶ added in v1.2.0
func (w *BufferedWriter) Close() error
func (*BufferedWriter) Flush ¶ added in v1.2.0
func (w *BufferedWriter) Flush() error
type Config ¶ added in v1.2.0
type Config struct { // New fields Level slog.Level // New: minimum log level JSON bool // New: output JSON? (true means JSON format) ServiceName string // New: service name to include in logs Environment string // New: e.g. "production" or "development" TimeFormat string // New: timestamp format MinLevel slog.Level AddSource bool JsonFormat bool AsyncWrite bool BufferSize int MaxFileSize int64 MaxBackups int ErrorCallback func(error) Metrics bool Output io.Writer FilePath string }
Config holds logger configuration
type Configurable ¶ added in v1.2.0
Configurable defines configuration management
type ContextProvider ¶ added in v1.2.0
type ContextProvider interface { WithContext(ctx context.Context) Logger GetContext() context.Context }
ContextProvider defines context management
type DirectWriter ¶ added in v1.2.0
type DirectWriter struct {
// contains filtered or unexported fields
}
DirectWriter implements Writer for synchronous writes
func NewDirectWriter ¶ added in v1.2.0
func NewDirectWriter(out io.Writer) *DirectWriter
func (*DirectWriter) Close ¶ added in v1.2.0
func (w *DirectWriter) Close() error
func (*DirectWriter) Flush ¶ added in v1.2.0
func (w *DirectWriter) Flush() error
type ErrorHandler ¶ added in v1.2.0
ErrorHandler manages error handling and recovery
type Factory ¶ added in v1.2.0
type Factory interface { CreateLogger(config Config) (Logger, error) CreateWriter(config Config) (Writer, error) CreateFormatter(config Config) (Formatter, error) CreateMetricsCollector(config Config) (MetricsCollector, error) CreateRotationManager(config Config) (RotationManager, error) CreateErrorHandler(config Config) (ErrorHandler, error) CreateBufferManager(config Config) (BufferManager, error) CreateHandlerWrapper(handler slog.Handler, closer io.Closer) HandlerWrapper CreateHealthChecker(logger Logger) (HealthChecker, error) }
Factory interface for creating logger components
func NewStandardFactory ¶ added in v1.2.0
func NewStandardFactory() Factory
type FieldProvider ¶ added in v1.2.0
type FieldProvider interface { WithFields(fields map[string]interface{}) Logger GetFields() map[string]interface{} }
FieldProvider defines structured field management
type FileWriter ¶ added in v1.2.0
type FileWriter struct {
// contains filtered or unexported fields
}
FileWriter implements Writer for file-based logging
func NewFileWriter ¶ added in v1.2.0
func NewFileWriter(config Config) (*FileWriter, error)
func (*FileWriter) Close ¶ added in v1.2.0
func (w *FileWriter) Close() error
func (*FileWriter) Flush ¶ added in v1.2.0
func (w *FileWriter) Flush() error
type FilterFunc ¶ added in v1.2.0
FilterFunc defines a function type for log filtering
type Filterable ¶ added in v1.2.0
Filterable defines log filtering capabilities
type Flushable ¶ added in v1.2.0
type Flushable interface {
Flush() error
}
Flushable defines the flush operation
type Formatter ¶ added in v1.2.0
type Formatter interface { Format(record LogRecord) ([]byte, error) WithOptions(opts FormatterOptions) Formatter }
Formatter handles log message formatting
type FormatterOptions ¶ added in v1.2.0
FormatterOptions defines formatting options
type HandlerWrapper ¶ added in v1.2.0
HandlerWrapper provides a wrapper for slog.Handler with lifecycle management
type HealthChecker ¶ added in v1.2.0
HealthChecker monitors logger health
type JSONFormatter ¶ added in v1.2.0
type JSONFormatter struct{}
Formatters
func NewJSONFormatter ¶ added in v1.2.0
func NewJSONFormatter() *JSONFormatter
func (*JSONFormatter) Format ¶ added in v1.2.0
func (f *JSONFormatter) Format(record LogRecord) ([]byte, error)
func (*JSONFormatter) WithOptions ¶ added in v1.2.0
func (f *JSONFormatter) WithOptions(opts FormatterOptions) Formatter
type Level ¶ added in v1.4.2
Level is an alias for slog.Level.
func ParseLevel ¶ added in v1.4.2
ParseLevel converts a string (case-insensitive) to the corresponding log Level. Supported values are "debug", "warn", "error"; all others default to LevelInfo.
type Lifecycle ¶ added in v1.2.0
type Lifecycle interface {
Close() error
}
Lifecycle defines common lifecycle methods
type LogRecord ¶ added in v1.2.0
type LogRecord struct { Level slog.Level Message string Time time.Time Source string Args []any Attrs []slog.Attr Context context.Context AddSource bool }
LogRecord represents a single log entry
type Logger ¶ added in v1.2.0
type Logger interface { Info(msg string, args ...any) Error(msg string, args ...any) Warn(msg string, args ...any) Debug(msg string, args ...any) With(args ...any) Logger WithFields(fields map[string]interface{}) Logger // Add this GetFields() map[string]interface{} // Add this Flush() error Close() error GetContext() context.Context WithContext(ctx context.Context) Logger }
Logger defines the main logging interface
func GetDefaultLogger ¶ added in v1.2.0
func GetDefaultLogger() Logger
GetDefaultLogger returns the current default logger instance
func NewLogger ¶ added in v1.4.4
NewLogger is a convenience function that creates a new logger using the standard factory and the provided configuration. It panics if the logger cannot be created.
func NewStandardLogger ¶ added in v1.2.0
NewStandardLogger creates a new logger instance using the provided factory
type Metrics ¶ added in v1.2.0
type Metrics struct { TotalMessages uint64 ErrorMessages uint64 DroppedMessages uint64 LastError time.Time LastFlush time.Time BufferSize int BufferUsage float64 }
Metrics holds logger operational Metrics
type MetricsCollector ¶ added in v1.2.0
type MetricsCollector interface { IncrementTotal() IncrementErrors() IncrementDropped() SetLastError(time.Time) SetLastFlush(time.Time) GetMetrics() Metrics Reset() }
MetricsCollector handles logging Metrics collection and reporting
type QueryOptions ¶ added in v1.2.0
type QueryOptions struct { Limit int Offset int StartTime time.Time EndTime time.Time OrderBy string Order string }
QueryOptions defines options for log querying
type Queryable ¶ added in v1.2.0
type Queryable interface {
Query(filter FilterFunc, opts QueryOptions) ([]LogRecord, error)
}
Queryable defines query capabilities for logs
type RateLimiter ¶ added in v1.2.0
RateLimiter defines rate limiting capabilities
type RotationManager ¶ added in v1.2.0
type RotationManager interface { ShouldRotate(size int64) bool Rotate() error Cleanup() error GetCurrentFile() (io.Writer, error) }
RotationManager handles log file rotation operations
type StackTraceLine ¶ added in v1.4.5
StackTraceLine represents a single line in a stack trace.
type StandardBufferManager ¶ added in v1.2.0
type StandardBufferManager struct {
// contains filtered or unexported fields
}
StandardBufferManager implements BufferManager
func NewStandardBufferManager ¶ added in v1.2.0
func NewStandardBufferManager(size int) *StandardBufferManager
func (*StandardBufferManager) Close ¶ added in v1.2.0
func (b *StandardBufferManager) Close() error
func (*StandardBufferManager) Flush ¶ added in v1.2.0
func (b *StandardBufferManager) Flush() error
func (*StandardBufferManager) IsFull ¶ added in v1.2.0
func (b *StandardBufferManager) IsFull() bool
type StandardErrorHandler ¶ added in v1.2.0
type StandardErrorHandler struct {
// contains filtered or unexported fields
}
Error Handler implementation
func NewStandardErrorHandler ¶ added in v1.2.0
func NewStandardErrorHandler(callback func(error)) *StandardErrorHandler
func (*StandardErrorHandler) Handle ¶ added in v1.2.0
func (h *StandardErrorHandler) Handle(err error)
func (*StandardErrorHandler) WithRecovery ¶ added in v1.2.0
func (h *StandardErrorHandler) WithRecovery(fn func() error) (err error)
type StandardFactory ¶ added in v1.2.0
type StandardFactory struct {
// contains filtered or unexported fields
}
StandardFactory implements the Factory interface
func (*StandardFactory) CreateBufferManager ¶ added in v1.2.0
func (f *StandardFactory) CreateBufferManager(config Config) (BufferManager, error)
func (*StandardFactory) CreateErrorHandler ¶ added in v1.2.0
func (f *StandardFactory) CreateErrorHandler(config Config) (ErrorHandler, error)
func (*StandardFactory) CreateFormatter ¶ added in v1.2.0
func (f *StandardFactory) CreateFormatter(config Config) (Formatter, error)
func (*StandardFactory) CreateHandlerWrapper ¶ added in v1.2.0
func (f *StandardFactory) CreateHandlerWrapper(handler slog.Handler, closer io.Closer) HandlerWrapper
func (*StandardFactory) CreateHealthChecker ¶ added in v1.2.0
func (f *StandardFactory) CreateHealthChecker(logger Logger) (HealthChecker, error)
func (*StandardFactory) CreateLogger ¶ added in v1.2.0
func (f *StandardFactory) CreateLogger(config Config) (Logger, error)
Modify the CreateLogger method in StandardFactory
func (*StandardFactory) CreateMetricsCollector ¶ added in v1.2.0
func (f *StandardFactory) CreateMetricsCollector(config Config) (MetricsCollector, error)
func (*StandardFactory) CreateRotationManager ¶ added in v1.2.0
func (f *StandardFactory) CreateRotationManager(config Config) (RotationManager, error)
func (*StandardFactory) CreateWriter ¶ added in v1.2.0
func (f *StandardFactory) CreateWriter(config Config) (Writer, error)
type StandardHandlerWrapper ¶ added in v1.2.0
type StandardHandlerWrapper struct {
// contains filtered or unexported fields
}
Handler Wrapper implementation
func NewStandardHandlerWrapper ¶ added in v1.2.0
func NewStandardHandlerWrapper(handler slog.Handler, closer io.Closer) *StandardHandlerWrapper
func (*StandardHandlerWrapper) Close ¶ added in v1.2.0
func (h *StandardHandlerWrapper) Close() error
type StandardHealthChecker ¶ added in v1.2.0
type StandardHealthChecker struct {
// contains filtered or unexported fields
}
Health Checker implementation
func NewStandardHealthChecker ¶ added in v1.2.0
func NewStandardHealthChecker(logger Logger) *StandardHealthChecker
func (*StandardHealthChecker) Check ¶ added in v1.2.0
func (h *StandardHealthChecker) Check() error
func (*StandardHealthChecker) Start ¶ added in v1.2.0
func (h *StandardHealthChecker) Start(ctx context.Context)
func (*StandardHealthChecker) Stop ¶ added in v1.2.0
func (h *StandardHealthChecker) Stop()
type StandardLogger ¶ added in v1.2.0
type StandardLogger struct { Metrics MetricsCollector // contains filtered or unexported fields }
StandardLogger implements the Logger interface
func (*StandardLogger) Close ¶ added in v1.2.0
func (l *StandardLogger) Close() error
func (*StandardLogger) Debug ¶ added in v1.2.0
func (l *StandardLogger) Debug(msg string, args ...any)
func (*StandardLogger) Error ¶ added in v1.2.0
func (l *StandardLogger) Error(msg string, args ...any)
func (*StandardLogger) Flush ¶ added in v1.2.0
func (l *StandardLogger) Flush() error
func (*StandardLogger) GetContext ¶ added in v1.2.0
func (l *StandardLogger) GetContext() context.Context
func (*StandardLogger) GetFields ¶ added in v1.2.0
func (l *StandardLogger) GetFields() map[string]interface{}
func (*StandardLogger) Info ¶ added in v1.2.0
func (l *StandardLogger) Info(msg string, args ...any)
Logger interface implementation
func (*StandardLogger) LogError ¶ added in v1.4.5
func (l *StandardLogger) LogError(ctx context.Context, err error)
LogError is a convenience method that logs an error with enhanced context. It simply adds the error (via WithError) and then logs it at error level.
func (*StandardLogger) RecoveryMiddleware ¶ added in v1.4.5
func (l *StandardLogger) RecoveryMiddleware() func(http.Handler) http.Handler
RecoveryMiddleware returns middleware that recovers from panics.
func (*StandardLogger) RequestLogger ¶ added in v1.4.5
func (l *StandardLogger) RequestLogger() func(http.Handler) http.Handler
RequestLogger returns middleware that logs HTTP requests.
func (*StandardLogger) Warn ¶ added in v1.2.0
func (l *StandardLogger) Warn(msg string, args ...any)
func (*StandardLogger) With ¶ added in v1.2.0
func (l *StandardLogger) With(args ...any) Logger
func (*StandardLogger) WithContext ¶ added in v1.2.0
func (l *StandardLogger) WithContext(ctx context.Context) Logger
Implement ContextProvider interface
func (*StandardLogger) WithError ¶ added in v1.4.5
func (l *StandardLogger) WithError(err error) Logger
WithError returns a new logger with the error field added. If the error is nil, it returns the current logger unchanged.
func (*StandardLogger) WithFields ¶ added in v1.2.0
func (l *StandardLogger) WithFields(fields map[string]interface{}) Logger
Implement FieldProvider interface
type StandardMetricsCollector ¶ added in v1.2.0
type StandardMetricsCollector struct {
// contains filtered or unexported fields
}
Metrics implementation
func NewStandardMetricsCollector ¶ added in v1.2.0
func NewStandardMetricsCollector() *StandardMetricsCollector
func (*StandardMetricsCollector) GetMetrics ¶ added in v1.2.0
func (m *StandardMetricsCollector) GetMetrics() Metrics
func (*StandardMetricsCollector) IncrementDropped ¶ added in v1.2.0
func (m *StandardMetricsCollector) IncrementDropped()
func (*StandardMetricsCollector) IncrementErrors ¶ added in v1.2.0
func (m *StandardMetricsCollector) IncrementErrors()
func (*StandardMetricsCollector) IncrementTotal ¶ added in v1.2.0
func (m *StandardMetricsCollector) IncrementTotal()
func (*StandardMetricsCollector) Reset ¶ added in v1.2.0
func (m *StandardMetricsCollector) Reset()
func (*StandardMetricsCollector) SetLastError ¶ added in v1.2.0
func (m *StandardMetricsCollector) SetLastError(t time.Time)
func (*StandardMetricsCollector) SetLastFlush ¶ added in v1.2.0
func (m *StandardMetricsCollector) SetLastFlush(t time.Time)
type StandardRotationManager ¶ added in v1.2.0
type StandardRotationManager struct {
// contains filtered or unexported fields
}
StandardRotationManager implements RotationManager
func NewStandardRotationManager ¶ added in v1.2.0
func NewStandardRotationManager(config Config) *StandardRotationManager
func (*StandardRotationManager) Cleanup ¶ added in v1.2.0
func (r *StandardRotationManager) Cleanup() error
func (*StandardRotationManager) GetCurrentFile ¶ added in v1.2.0
func (r *StandardRotationManager) GetCurrentFile() (io.Writer, error)
func (*StandardRotationManager) Rotate ¶ added in v1.2.0
func (r *StandardRotationManager) Rotate() error
func (*StandardRotationManager) ShouldRotate ¶ added in v1.2.0
func (r *StandardRotationManager) ShouldRotate(size int64) bool
type TextFormatter ¶ added in v1.2.0
type TextFormatter struct {
// contains filtered or unexported fields
}
func NewTextFormatter ¶ added in v1.2.0
func NewTextFormatter() *TextFormatter
func (*TextFormatter) Format ¶ added in v1.2.0
func (f *TextFormatter) Format(record LogRecord) ([]byte, error)
func (*TextFormatter) WithOptions ¶ added in v1.2.0
func (f *TextFormatter) WithOptions(opts FormatterOptions) Formatter
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
example
|
|
custom
File: example/custom/main.go
|
File: example/custom/main.go |
security-logging
File: example/security-logging/main.go
|
File: example/security-logging/main.go |
structured-logging
File: example/structured-logging/main.go
|
File: example/structured-logging/main.go |