logging

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddSpanID

func AddSpanID(ctx context.Context, spanID string) context.Context

AddSpanID adds span ID to context.

func AddTraceID

func AddTraceID(ctx context.Context, traceID string) context.Context

AddTraceID adds trace ID to context.

func GetSpanID

func GetSpanID(ctx context.Context) string

GetSpanID extracts span ID from context.

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID extracts trace ID from context.

func LogCallerInfo

func LogCallerInfo(depth int) (string, string)

LogCallerInfo captures caller information.

func LogInterceptorFunc

func LogInterceptorFunc(logger *Logger) func(http.Handler) http.Handler

LogInterceptorFunc creates a simple middleware for standard libraries.

func RotationConfigToYAML

func RotationConfigToYAML() string

RotationConfigToYAML generates a sample log rotation config.

Types

type ContextKey

type ContextKey string

ContextKey type for log context values.

const (
	TraceIDKey ContextKey = "trace_id"
	SpanIDKey  ContextKey = "span_id"
)

type Interceptor

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

Interceptor wraps handlers with structured logging.

func NewInterceptor

func NewInterceptor(logger *Logger) *Interceptor

NewInterceptor creates a new logging interceptor.

func (*Interceptor) MiddlewareFunc

func (i *Interceptor) MiddlewareFunc(next http.Handler) http.Handler

MiddlewareFunc returns a middleware function for chi/echo/etc.

func (*Interceptor) WrapFunc

func (i *Interceptor) WrapFunc(next http.HandlerFunc) http.HandlerFunc

WrapFunc wraps an HTTP handler function with logging.

func (*Interceptor) WrapHandler

func (i *Interceptor) WrapHandler(next http.Handler) http.Handler

WrapHandler wraps an HTTP handler with logging.

type JSONLogSchema

type JSONLogSchema struct {
	Timestamp  string                 `json:"timestamp"`
	Level      string                 `json:"level"`
	Message    string                 `json:"message"`
	Caller     string                 `json:"caller,omitempty"`
	Attributes map[string]interface{} `json:"attributes,omitempty"`
	TraceID    string                 `json:"trace_id,omitempty"`
	SpanID     string                 `json:"span_id,omitempty"`
}

JSONLogSchema defines the structured log format.

func (*JSONLogSchema) LogJSON

func (l *JSONLogSchema) LogJSON() ([]byte, error)

LogJSON marshals the log entry to JSON.

type LogConfig

type LogConfig struct {
	Level      slog.Level `default:"info"`
	Output     string     `default:"stdout"`
	Directory  string     `default:"logs"`
	MaxSizeMB  int        `default:"100"`
	MaxAgeDays int        `default:"30"`
	MaxBackups int        `default:"10"`
}

LogConfig holds logging configuration.

type LogFileInfo

type LogFileInfo struct {
	Name         string
	Path         string
	SizeBytes    int64
	ModTime      time.Time
	IsCompressed bool
}

LogFileInfo represents information about a log file.

type LogManager

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

LogManager handles log file management.

func NewLogManager

func NewLogManager(cfg LogRotationConfig, retention RetentionPolicy) *LogManager

NewLogManager creates a new log manager.

func (*LogManager) Cleanup

func (m *LogManager) Cleanup() error

Cleanup removes old log files based on retention policy.

func (*LogManager) GetTotalSize

func (m *LogManager) GetTotalSize() (int64, error)

GetTotalSize returns total size of all log files.

func (*LogManager) ListLogFiles

func (m *LogManager) ListLogFiles() ([]LogFileInfo, error)

ListLogFiles returns all log files in the directory.

type LogRotationConfig

type LogRotationConfig struct {
	Directory  string `yaml:"directory" json:"directory"`
	MaxSizeMB  int    `yaml:"max_size_mb" json:"max_size_mb"`
	MaxAgeDays int    `yaml:"max_age_days" json:"max_age_days"`
	MaxBackups int    `yaml:"max_backups" json:"max_backups"`
	Compress   bool   `yaml:"compress" json:"compress"`
	Pattern    string `yaml:"pattern" json:"pattern"` // e.g., "app-*.log"
}

LogRotationConfig holds configuration for log rotation.

type Logger

type Logger struct {
	*slog.Logger
	// contains filtered or unexported fields
}

Logger wraps slog with additional functionality.

func NewLogger

func NewLogger(cfg LogConfig) *Logger

NewLogger creates a new structured logger.

func (*Logger) WithAttrs

func (l *Logger) WithAttrs(attrs map[string]interface{}) *Logger

WithAttrs returns a logger with additional attributes.

func (*Logger) WithTraceID

func (l *Logger) WithTraceID(traceID string) *Logger

WithTraceID returns a logger with trace ID.

type RetentionPolicy

type RetentionPolicy struct {
	KeepDays         int      `yaml:"keep_days" json:"keep_days"`
	KeepBackups      int      `yaml:"keep_backups" json:"keep_backups"`
	CompressOldLogs  bool     `yaml:"compress_old_logs" json:"compress_old_logs"`
	ExcludedPatterns []string `yaml:"excluded_patterns" json:"excluded_patterns"` // e.g., ["debug.log"]
}

RetentionPolicy defines log retention rules.

type RotationPolicy

type RotationPolicy struct {
	MaxSizeBytes int64
	MaxAgeDays   int
	Schedule     string // "daily", "hourly", or cron expression
}

RotationPolicy defines when to rotate logs.

func DefaultRotationPolicy

func DefaultRotationPolicy() RotationPolicy

DefaultRotationPolicy returns the default rotation policy.

Jump to

Keyboard shortcuts

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