logger

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRequestID

func GetRequestID(ctx context.Context) (string, bool)

GetRequestID retrieves the request ID from context

func WithContext

func WithContext(ctx context.Context, requestID string) context.Context

WithContext returns a new context with the request ID

Types

type Config

type Config struct {
	// Logger is the logger implementation to use
	Logger Logger
	// Level determines the logging verbosity
	Level LogLevel
	// Format defines the output format (text or JSON)
	Format OutputFormat
	// RequestIDGenerator creates unique request identifiers
	RequestIDGenerator func() string
	// RequestIDHeader is the header name for propagating request IDs
	RequestIDHeader string
	// SensitiveHeaders are headers that should be redacted
	SensitiveHeaders []string
	// SkipPaths are URL paths that should not be logged
	SkipPaths []string
	// SensitiveFields are JSON fields that should be redacted in bodies
	SensitiveFields []string
	// EnableSampling enables log sampling to reduce volume
	EnableSampling bool
	// SampleRate defines the log sampling rate (1.0 = 100%)
	SampleRate float64
	// PropagateRequestID controls whether to propagate request ID
	PropagateRequestID bool
}

Config holds the configuration for the logger middleware

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a default configuration

type ContextKey

type ContextKey string

ContextKey type for context value storage

const (
	// RequestIDKey is the context key for storing request IDs
	RequestIDKey ContextKey = "request_id"
	// MaxBodyLogSize limits the body size in logs
	MaxBodyLogSize = 10 * 1024 // 10KB
)

type LogLevel

type LogLevel int

LogLevel defines logging verbosity

const (
	// LevelNone disables logging
	LevelNone LogLevel = iota
	// LevelError logs only errors
	LevelError
	// LevelInfo logs basic request/response info without headers/body
	LevelInfo
	// LevelDebug logs with headers but no bodies
	LevelDebug
	// LevelTrace logs everything including bodies
	LevelTrace
)

func (LogLevel) String

func (l LogLevel) String() string

String returns a string representation of the log level

type Logger

type Logger interface {
	Log(ctx context.Context, level LogLevel, msg string, fields map[string]interface{})
}

Logger interface allows for integration with any logging library

type Middleware

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

Middleware implements HTTP client logging

func New

func New(config *Config) *Middleware

New creates a new logger middleware

func WithJSON

func WithJSON() *Middleware

WithJSON returns a middleware with JSON output format

func WithLevel

func WithLevel(level LogLevel) *Middleware

WithLevel returns a middleware with the specified log level

func (*Middleware) Handle

Handle implements the middleware.Handler interface

type OutputFormat

type OutputFormat int

OutputFormat defines how logs are formatted

const (
	// FormatText outputs logs in human-readable text format
	FormatText OutputFormat = iota
	// FormatJSON outputs logs in JSON format
	FormatJSON
)

type StandardLogger

type StandardLogger struct {
	Level  LogLevel
	Format OutputFormat
}

StandardLogger is a simple logger that writes to stdout

func (*StandardLogger) Log

func (l *StandardLogger) Log(ctx context.Context, level LogLevel, msg string, fields map[string]interface{})

Log implements the Logger interface

Jump to

Keyboard shortcuts

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