Skip to content

httplog v2.0.0

Latest

Choose a tag to compare

@erudenko erudenko released this 21 Jan 04:32
v2.0.0
aa73951

httplog v2.0.0

A major release with significant API improvements, new features, and better error handling.

⚠️ Breaking Changes

Module Path

// Old
import "github.com/MadAppGang/httplog"

// New
import "github.com/MadAppGang/httplog/v2"

Factory Functions Return Errors

All Logger*() and Handler*() functions now return (*LoggingMiddleware, error):

// Old
logger := httplog.LoggerWithName("API")
http.Handle("/", logger(handler))

// New
logger, err := httplog.LoggerWithName("API")
if err != nil {
    panic(err)
}
defer logger.Close() // Important for async logging!
http.Handle("/", logger.Handler(handler))

Removed Global Color Functions

// Old - removed
httplog.DisableConsoleColor()
httplog.ForceConsoleColor()

// New - use config
httplog.LoggerWithConfig(httplog.LoggerConfig{
    ColorMode: httplog.ColorDisable, // or ColorForce, ColorAuto
})

Renamed Fields

Old New
CaptureBody CaptureResponseBody
LogFormatterParams.Body LogFormatterParams.ResponseBody

Removed

  • FormatterFunction type alias (use LogFormatter directly)
  • http.CloseNotifier support (deprecated since Go 1.11)

Minimum Go Version

  • Now requires Go 1.21+

✨ New Features

LoggingMiddleware with Graceful Shutdown

logger, _ := httplog.LoggerWithConfig(httplog.LoggerConfig{
    AsyncLogging: true,
})
defer logger.Close() // Cleanly stops async goroutine

Request Sampling

httplog.LoggerWithConfig(httplog.LoggerConfig{
    SampleRate: 0.1, // Log only 10% of requests
    DeterministicSampling: true, // Hash-based (reproducible)
})

Log Level Filtering

httplog.LoggerWithConfig(httplog.LoggerConfig{
    MinLevel: httplog.LevelWarn, // Only log 4xx and 5xx
})

Request Body Capture

httplog.LoggerWithConfig(httplog.LoggerConfig{
    CaptureRequestBody: true,
})

Async Logging

httplog.LoggerWithConfig(httplog.LoggerConfig{
    AsyncLogging: true,
    AsyncBufferSize: 5000,
})

New LogFormatterParams Fields

  • Context - for trace ID extraction
  • RequestBody - captured request body
  • RequestHeader - request headers (with masking)
  • Level - log level (Debug, Info, Warn, Error)

ConfigBuilder Fluent API

config := httplog.NewConfigBuilder().
    WithName("API").
    WithColorMode(httplog.ColorForce).
    WithSampleRate(0.5).
    Build()

slog Integration

logger := slog.Default()
formatter := httplog.SlogFormatter(logger, slog.LevelInfo, "HTTP")

🔧 Improvements

  • Better validation: ValidateConfig() catches errors early
  • Pattern index in errors: invalid SkipPaths[2] regex pattern
  • ColorAuto fully resolved: No longer leaks through to formatters
  • Request body errors handled: Gracefully captured as [body read error: ...]
  • Performance: ChainLogFormatter uses strings.Builder

📦 Installation

go get github.com/MadAppGang/httplog/v2@v2.0.0

📚 Full Documentation

See the README for complete usage examples.