Documentation
¶
Overview ¶
Package logging provides structured logging capabilities using Go's log/slog package.
It includes a Logger wrapper with convenience methods for operations and components, structured error logging for SyncError types, and utilities for context propagation. Initialize with Init() to set the global logger instance.
See also:
- README: https://github.com/c0deZ3R0/go-sync-kit#readme
- Architecture overview: https://github.com/c0deZ3R0/go-sync-kit/blob/main/docs/overview.md
Package logging provides structured logging capabilities using Go's log/slog package following best practices from the Better Stack Community Guide.
Index ¶
- Constants
- Variables
- func Debug(msg string, attrs ...slog.Attr)
- func DebugContext(ctx context.Context, msg string, attrs ...slog.Attr)
- func Error(msg string, attrs ...slog.Attr)
- func ErrorContext(ctx context.Context, msg string, attrs ...slog.Attr)
- func Fatal(msg string, attrs ...slog.Attr)
- func FatalContext(ctx context.Context, msg string, attrs ...slog.Attr)
- func Info(msg string, attrs ...slog.Attr)
- func InfoContext(ctx context.Context, msg string, attrs ...slog.Attr)
- func Init(config Config)
- func LogError(ctx context.Context, err error, msg string, attrs ...slog.Attr)
- func LogOperation(ctx context.Context, op Operation, component Component, fn func() error) error
- func NewLoggerWithDynamicLevel(config Config) (*Logger, *DynamicLevelVar)
- func Trace(msg string, attrs ...slog.Attr)
- func TraceContext(ctx context.Context, msg string, attrs ...slog.Attr)
- func Warn(msg string, attrs ...slog.Attr)
- func WarnContext(ctx context.Context, msg string, attrs ...slog.Attr)
- type Component
- type Config
- type CustomLevel
- type DynamicLevelVar
- type Logger
- func (l *Logger) LogError(ctx context.Context, err error, msg string, attrs ...slog.Attr)
- func (l *Logger) LogOperation(ctx context.Context, op Operation, component Component, fn func() error) error
- func (l *Logger) WithComponent(component Component) *Logger
- func (l *Logger) WithContext(ctx context.Context, attrs ...slog.Attr) *Logger
- func (l *Logger) WithOperation(op Operation) *Logger
- type Operation
- type SyncErrorValuer
Constants ¶
const ( EnvDevelopment = "development" EnvProduction = "production" EnvTest = "test" )
Environment types
Variables ¶
var DefaultConfig = Config{ Level: "info", Format: "json", AddSource: true, Environment: "dev", }
Default configuration
Functions ¶
func FatalContext ¶
FatalContext logs at fatal level with context and exits the program
func Init ¶
func Init(config Config)
Init initializes the global logger with the provided configuration
func LogOperation ¶
func NewLoggerWithDynamicLevel ¶
func NewLoggerWithDynamicLevel(config Config) (*Logger, *DynamicLevelVar)
NewLoggerWithDynamicLevel creates a logger with dynamic level support
func TraceContext ¶
TraceContext logs at trace level with context using the default logger
Types ¶
type Config ¶
type Config struct {
Level string `json:"level"` // debug, info, warn, error
Format string `json:"format"` // text, json
AddSource bool `json:"add_source"` // whether to add source code information
Environment string `json:"environment"` // dev, prod, test
}
Config holds logger configuration
func GetConfigFromEnv ¶
func GetConfigFromEnv() Config
GetConfigFromEnv creates a logger configuration based on environment variables
type CustomLevel ¶
CustomLevel defines a custom log level between existing ones
const ( LevelTrace CustomLevel = CustomLevel(slog.LevelDebug - 4) // Even more verbose than debug LevelFatal CustomLevel = CustomLevel(slog.LevelError + 4) // More severe than error )
Custom levels between the standard ones
func (CustomLevel) String ¶
func (l CustomLevel) String() string
String returns the string representation of the custom level
type DynamicLevelVar ¶
DynamicLevelVar allows changing log level at runtime
func NewDynamicLevelVar ¶
func NewDynamicLevelVar(initialLevel slog.Level) *DynamicLevelVar
NewDynamicLevelVar creates a new dynamic level variable
func (*DynamicLevelVar) SetFromString ¶
func (d *DynamicLevelVar) SetFromString(level string) bool
SetFromString sets the level from a string representation
type Logger ¶
Logger is our wrapper around slog.Logger with additional convenience methods
func WithComponent ¶
func WithOperation ¶
func (*Logger) LogError ¶
LogError logs an error with stack trace information and structured attributes
func (*Logger) LogOperation ¶
func (l *Logger) LogOperation(ctx context.Context, op Operation, component Component, fn func() error) error
LogOperation logs the start and end of an operation with duration tracking
func (*Logger) WithComponent ¶
WithComponent creates a child logger with component context
func (*Logger) WithContext ¶
WithContext creates a child logger with key-value context
func (*Logger) WithOperation ¶
WithOperation creates a child logger with operation context
type Operation ¶
type Operation string
LogValuer implementations for consistent representation of custom types
type SyncErrorValuer ¶
SyncErrorValuer provides structured logging for SyncError
func (SyncErrorValuer) LogValue ¶
func (e SyncErrorValuer) LogValue() slog.Value