Documentation
¶
Index ¶
- func AddSpanID(ctx context.Context, spanID string) context.Context
- func AddTraceID(ctx context.Context, traceID string) context.Context
- func GetSpanID(ctx context.Context) string
- func GetTraceID(ctx context.Context) string
- func LogCallerInfo(depth int) (string, string)
- func LogInterceptorFunc(logger *Logger) func(http.Handler) http.Handler
- func RotationConfigToYAML() string
- type ContextKey
- type Interceptor
- type JSONLogSchema
- type LogConfig
- type LogFileInfo
- type LogManager
- type LogRotationConfig
- type Logger
- type RetentionPolicy
- type RotationPolicy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddTraceID ¶
AddTraceID adds trace ID to context.
func GetTraceID ¶
GetTraceID extracts trace ID from context.
func LogCallerInfo ¶
LogCallerInfo captures caller information.
func LogInterceptorFunc ¶
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 ¶
Logger wraps slog with additional functionality.
func (*Logger) WithTraceID ¶
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.