logging

package
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(message string, fields ...map[string]interface{})

Debug logs a debug message using the default logger

func Debugf

func Debugf(format string, args ...interface{})

Debugf logs a formatted debug message using the default logger

func Error

func Error(message string, fields ...map[string]interface{})

Error logs an error message using the default logger

func Errorf

func Errorf(format string, args ...interface{})

Errorf logs a formatted error message using the default logger

func Fatal

func Fatal(message string, fields ...map[string]interface{})

Fatal logs a fatal message using the default logger and exits

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf logs a formatted fatal message using the default logger and exits

func HealthHandler

func HealthHandler(monitor *HealthMonitor) http.HandlerFunc

HealthHandler returns an HTTP handler for health endpoint

func Info

func Info(message string, fields ...map[string]interface{})

Info logs an info message using the default logger

func Infof

func Infof(format string, args ...interface{})

Infof logs a formatted info message using the default logger

func LivenessHandler

func LivenessHandler() http.HandlerFunc

LivenessHandler returns an HTTP handler for liveness endpoint

func MetricsHandler

func MetricsHandler(registry *MetricRegistry) http.HandlerFunc

MetricsHandler returns an HTTP handler for metrics endpoint

func MetricsMiddleware

func MetricsMiddleware(metrics *ApplicationMetrics, logger *Logger) func(next http.Handler) http.Handler

MetricsMiddleware creates middleware that automatically tracks HTTP metrics

func ReadinessHandler

func ReadinessHandler() http.HandlerFunc

ReadinessHandler returns an HTTP handler for readiness endpoint

func RecoveryMiddleware

func RecoveryMiddleware(logger *Logger) func(next http.Handler) http.Handler

RecoveryMiddleware creates structured logging recovery middleware

func SetDefaultLogger

func SetDefaultLogger(logger *Logger)

SetDefaultLogger sets the global default logger

func StructuredLoggingMiddleware

func StructuredLoggingMiddleware(logger *Logger) func(next http.Handler) http.Handler

StructuredLoggingMiddleware creates middleware that adds structured logger to request context

func ToContext

func ToContext(ctx context.Context, logger *Logger) context.Context

ToContext adds a logger to context

func Trace

func Trace(message string, fields ...map[string]interface{})

Trace logs a trace message using the default logger

func Tracef

func Tracef(format string, args ...interface{})

Tracef logs a formatted trace message using the default logger

func Warn

func Warn(message string, fields ...map[string]interface{})

Warn logs a warning message using the default logger

func Warnf

func Warnf(format string, args ...interface{})

Warnf logs a formatted warning message using the default logger

Types

type ApplicationMetrics

type ApplicationMetrics struct {
	RequestsTotal       *Counter
	RequestDuration     *Histogram
	ResponseStatusCodes *Counter
	ActiveConnections   *Gauge
	ErrorsTotal         *Counter
}

ApplicationMetrics holds commonly used application metrics

func NewApplicationMetrics

func NewApplicationMetrics() *ApplicationMetrics

NewApplicationMetrics creates application metrics

func (*ApplicationMetrics) Register

func (am *ApplicationMetrics) Register(registry *MetricRegistry)

Register registers all application metrics

type Config

type Config struct {
	Level      LogLevel
	Writer     io.Writer
	Service    string
	EnableJSON bool
}

Config holds logger configuration

type Counter

type Counter struct {
	// contains filtered or unexported fields
}

Counter represents a counter metric

func NewCounter

func NewCounter(name string, labels map[string]string) *Counter

NewCounter creates a new counter

func (*Counter) Add

func (c *Counter) Add(delta int64)

func (*Counter) Get

func (c *Counter) Get() int64

func (*Counter) Inc

func (c *Counter) Inc()

func (*Counter) Labels

func (c *Counter) Labels() map[string]string

func (*Counter) Name

func (c *Counter) Name() string

func (*Counter) Type

func (c *Counter) Type() MetricType

func (*Counter) Value

func (c *Counter) Value() interface{}

type Gauge

type Gauge struct {
	// contains filtered or unexported fields
}

Gauge represents a gauge metric

func NewGauge

func NewGauge(name string, labels map[string]string) *Gauge

NewGauge creates a new gauge

func (*Gauge) Add

func (g *Gauge) Add(delta int64)

func (*Gauge) Dec

func (g *Gauge) Dec()

func (*Gauge) Get

func (g *Gauge) Get() int64

func (*Gauge) Inc

func (g *Gauge) Inc()

func (*Gauge) Labels

func (g *Gauge) Labels() map[string]string

func (*Gauge) Name

func (g *Gauge) Name() string

func (*Gauge) Set

func (g *Gauge) Set(value int64)

func (*Gauge) Sub

func (g *Gauge) Sub(delta int64)

func (*Gauge) Type

func (g *Gauge) Type() MetricType

func (*Gauge) Value

func (g *Gauge) Value() interface{}

type HealthCheck

type HealthCheck struct {
	Status  string      `json:"status"`
	Message string      `json:"message,omitempty"`
	Details interface{} `json:"details,omitempty"`
}

HealthCheck represents an individual health check

type HealthChecker

type HealthChecker func() HealthCheck

HealthChecker function type for health checks

func DatabaseHealthChecker

func DatabaseHealthChecker(pingFunc func() error) HealthChecker

DatabaseHealthChecker creates a health checker for database connectivity

func FileSystemHealthChecker

func FileSystemHealthChecker(path string) HealthChecker

FileSystemHealthChecker creates a health checker for file system access

func RedisHealthChecker

func RedisHealthChecker(pingFunc func() error) HealthChecker

RedisHealthChecker creates a health checker for Redis connectivity

type HealthMonitor

type HealthMonitor struct {
	// contains filtered or unexported fields
}

HealthMonitor manages health checks

func GetDefaultHealthMonitor

func GetDefaultHealthMonitor() *HealthMonitor

GetDefaultHealthMonitor returns the default health monitor

func NewHealthMonitor

func NewHealthMonitor(version string) *HealthMonitor

NewHealthMonitor creates a new health monitor

func (*HealthMonitor) AddCheck

func (hm *HealthMonitor) AddCheck(name string, checker HealthChecker)

AddCheck adds a health check

func (*HealthMonitor) CheckHealth

func (hm *HealthMonitor) CheckHealth() HealthStatus

CheckHealth performs all health checks

func (*HealthMonitor) RemoveCheck

func (hm *HealthMonitor) RemoveCheck(name string)

RemoveCheck removes a health check

type HealthStatus

type HealthStatus struct {
	Status    string                 `json:"status"`
	Timestamp time.Time              `json:"timestamp"`
	Version   string                 `json:"version,omitempty"`
	Uptime    string                 `json:"uptime"`
	Checks    map[string]HealthCheck `json:"checks,omitempty"`
}

HealthStatus represents the health status of the application

type Histogram

type Histogram struct {
	// contains filtered or unexported fields
}

Histogram represents a histogram metric

func NewHistogram

func NewHistogram(name string, labels map[string]string) *Histogram

NewHistogram creates a new histogram with default buckets

func NewHistogramWithBuckets

func NewHistogramWithBuckets(name string, labels map[string]string, buckets []float64) *Histogram

NewHistogramWithBuckets creates a new histogram with custom buckets

func (*Histogram) Labels

func (h *Histogram) Labels() map[string]string

func (*Histogram) Name

func (h *Histogram) Name() string

func (*Histogram) Observe

func (h *Histogram) Observe(value float64)

Observe records an observation

func (*Histogram) Type

func (h *Histogram) Type() MetricType

func (*Histogram) Value

func (h *Histogram) Value() interface{}

type LogEntry

type LogEntry struct {
	Timestamp time.Time              `json:"timestamp"`
	Level     string                 `json:"level"`
	Message   string                 `json:"message"`
	Fields    map[string]interface{} `json:"fields,omitempty"`
	Caller    string                 `json:"caller,omitempty"`
	RequestID string                 `json:"request_id,omitempty"`
	UserID    string                 `json:"user_id,omitempty"`
	Service   string                 `json:"service,omitempty"`
}

LogEntry represents a structured log entry

type LogLevel

type LogLevel int

LogLevel represents the severity of a log entry

const (
	TraceLevel LogLevel = iota
	DebugLevel
	InfoLevel
	WarnLevel
	ErrorLevel
	FatalLevel
)

func ParseLogLevel

func ParseLogLevel(level string) LogLevel

ParseLogLevel converts a string to LogLevel

func (LogLevel) String

func (l LogLevel) String() string

String returns the string representation of the log level

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger represents a structured logger

func FromContext

func FromContext(ctx context.Context) *Logger

FromContext retrieves a logger from context or returns the default logger

func GetDefaultLogger

func GetDefaultLogger() *Logger

GetDefaultLogger returns the global default logger

func New

func New(config Config) *Logger

New creates a new Logger instance

func NewDefault

func NewDefault() *Logger

NewDefault creates a logger with sensible defaults

func (*Logger) Debug

func (l *Logger) Debug(message string, fields ...map[string]interface{})

Debug logs a debug message

func (*Logger) Debugf

func (l *Logger) Debugf(format string, args ...interface{})

Debugf logs a formatted debug message

func (*Logger) Error

func (l *Logger) Error(message string, fields ...map[string]interface{})

Error logs an error message

func (*Logger) Errorf

func (l *Logger) Errorf(format string, args ...interface{})

Errorf logs a formatted error message

func (*Logger) Fatal

func (l *Logger) Fatal(message string, fields ...map[string]interface{})

Fatal logs a fatal message and exits

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, args ...interface{})

Fatalf logs a formatted fatal message and exits

func (*Logger) GetLevel

func (l *Logger) GetLevel() LogLevel

GetLevel returns the current log level

func (*Logger) Info

func (l *Logger) Info(message string, fields ...map[string]interface{})

Info logs an info message

func (*Logger) Infof

func (l *Logger) Infof(format string, args ...interface{})

Infof logs a formatted info message

func (*Logger) SetLevel

func (l *Logger) SetLevel(level LogLevel)

SetLevel sets the minimum log level

func (*Logger) Trace

func (l *Logger) Trace(message string, fields ...map[string]interface{})

Trace logs a trace message

func (*Logger) Tracef

func (l *Logger) Tracef(format string, args ...interface{})

Tracef logs a formatted trace message

func (*Logger) Warn

func (l *Logger) Warn(message string, fields ...map[string]interface{})

Warn logs a warning message

func (*Logger) Warnf

func (l *Logger) Warnf(format string, args ...interface{})

Warnf logs a formatted warning message

func (*Logger) WithField

func (l *Logger) WithField(key string, value interface{}) *Logger

WithField returns a new logger with an additional field

func (*Logger) WithFields

func (l *Logger) WithFields(fields map[string]interface{}) *Logger

WithFields returns a new logger with additional fields

func (*Logger) WithRequestID

func (l *Logger) WithRequestID(requestID string) *Logger

WithRequestID returns a new logger with request ID

func (*Logger) WithUserID

func (l *Logger) WithUserID(userID string) *Logger

WithUserID returns a new logger with user ID

type Metric

type Metric interface {
	Type() MetricType
	Name() string
	Value() interface{}
	Labels() map[string]string
}

Metric represents a metric

type MetricRegistry

type MetricRegistry struct {
	// contains filtered or unexported fields
}

MetricRegistry manages metrics

func GetDefaultRegistry

func GetDefaultRegistry() *MetricRegistry

GetDefaultRegistry returns the default metric registry

func NewMetricRegistry

func NewMetricRegistry() *MetricRegistry

NewMetricRegistry creates a new metric registry

func (*MetricRegistry) Get

func (r *MetricRegistry) Get(name string) (Metric, bool)

Get retrieves a metric by name

func (*MetricRegistry) GetAll

func (r *MetricRegistry) GetAll() map[string]Metric

GetAll returns all metrics

func (*MetricRegistry) Register

func (r *MetricRegistry) Register(metric Metric)

Register registers a metric

func (*MetricRegistry) Unregister

func (r *MetricRegistry) Unregister(name string)

Unregister removes a metric

type MetricType

type MetricType string

MetricType represents the type of metric

const (
	CounterType   MetricType = "counter"
	GaugeType     MetricType = "gauge"
	HistogramType MetricType = "histogram"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL