Documentation
¶
Overview ¶
Package logger provides structured logging with request tracing and service context. It uses zap for high-performance logging with support for different output formats.
Index ¶
- func LogDebug(msg string, fields ...Field)
- func LogError(msg string, err error, fields ...Field)
- func LogFatal(msg string, fields ...Field)
- func LogInfo(msg string, fields ...Field)
- func LogWarn(msg string, fields ...Field)
- func SetGlobalLogger(logger Logger)
- type BatchLogger
- type Field
- func Any(key string, value interface{}) Field
- func Bool(key string, value bool) Field
- func Component(value string) Field
- func Duration(key string, value interface{}) Field
- func Error(err error) Field
- func Float64(key string, value float64) Field
- func Int(key string, value int) Field
- func Int64(key string, value int64) Field
- func Namespace(value string) Field
- func Stack() Field
- func String(key, value string) Field
- func Time(key string, value time.Time) Field
- type LogEntry
- type Logger
- func GetGlobalLogger() Logger
- func New(cfg config.LoggingConfig) Logger
- func NewDevelopment() Logger
- func NewProduction() Logger
- func NewWithRotation(cfg config.LoggingConfig) Logger
- func WithContext(ctx context.Context) Logger
- func WithRequestID(requestID string) Logger
- func WithService(service string) Logger
- type LoggerManager
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetGlobalLogger ¶
func SetGlobalLogger(logger Logger)
SetGlobalLogger sets the global logger instance.
Types ¶
type BatchLogger ¶
type BatchLogger struct {
// contains filtered or unexported fields
}
BatchLogger provides batched logging capabilities.
func NewBatchLogger ¶
func NewBatchLogger(logger Logger, batchSize int) *BatchLogger
NewBatchLogger creates a new batch logger.
func (*BatchLogger) Add ¶
func (bl *BatchLogger) Add(entry LogEntry)
Add adds a log entry to the batch.
type Field ¶
type Field struct {
Key string
Value interface{}
}
Field represents a key-value pair for structured logging.
type LogEntry ¶
type LogEntry struct {
Level string `json:"level"`
Timestamp time.Time `json:"timestamp"`
Message string `json:"message"`
Fields map[string]interface{} `json:"fields"`
Service string `json:"service,omitempty"`
RequestID string `json:"request_id,omitempty"`
UserID string `json:"user_id,omitempty"`
TenantID string `json:"tenant_id,omitempty"`
Error string `json:"error,omitempty"`
}
LogEntry represents a structured log entry for batch logging.
type Logger ¶
type Logger interface {
// Info logs an info level message with optional fields
Info(msg string, fields ...Field)
// Error logs an error level message with optional fields
Error(msg string, err error, fields ...Field)
// Warn logs a warning level message with optional fields
Warn(msg string, fields ...Field)
// Debug logs a debug level message with optional fields
Debug(msg string, fields ...Field)
// Fatal logs a fatal level message and exits the program
Fatal(msg string, fields ...Field)
// WithRequestID returns a logger with request ID context
WithRequestID(requestID string) Logger
// WithCorrelationID returns a logger with correlation ID context
WithCorrelationID(correlationID string) Logger
// WithService returns a logger with service name context
WithService(service string) Logger
// WithTenant returns a logger with tenant ID context
WithTenant(tenantID string) Logger
// WithUser returns a logger with user ID context
WithUser(userID string) Logger
// WithFields returns a logger with additional fields
WithFields(fields ...Field) Logger
// WithContext returns a logger with context values
WithContext(ctx context.Context) Logger
// SetLevel dynamically changes the log level
SetLevel(level string) error
// Sync flushes any buffered log entries
Sync() error
// Clone creates a copy of the logger
Clone() Logger
}
Logger defines the interface for structured logging.
func GetGlobalLogger ¶
func GetGlobalLogger() Logger
GetGlobalLogger returns the global logger instance.
func New ¶
func New(cfg config.LoggingConfig) Logger
New creates a new logger instance with the given configuration.
func NewDevelopment ¶
func NewDevelopment() Logger
NewDevelopment creates a development logger with console output.
func NewProduction ¶
func NewProduction() Logger
NewProduction creates a production logger with JSON output.
func NewWithRotation ¶
func NewWithRotation(cfg config.LoggingConfig) Logger
NewWithRotation creates a new logger with log rotation support.
func WithContext ¶
WithContext returns a logger with context using the global logger.
func WithRequestID ¶
WithRequestID returns a logger with request ID using the global logger.
func WithService ¶
WithService returns a logger with service name using the global logger.
type LoggerManager ¶
type LoggerManager struct {
// contains filtered or unexported fields
}
LoggerManager manages multiple loggers and provides advanced logging features.
func (*LoggerManager) GetLogger ¶
func (lm *LoggerManager) GetLogger(name string) Logger
GetLogger returns a named logger instance.
func (*LoggerManager) SetDefaultLogger ¶
func (lm *LoggerManager) SetDefaultLogger(logger Logger)
SetDefaultLogger sets the default logger.