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
FormatterFunctiontype alias (useLogFormatterdirectly)http.CloseNotifiersupport (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 goroutineRequest 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 extractionRequestBody- captured request bodyRequestHeader- 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:
ChainLogFormatterusesstrings.Builder
📦 Installation
go get github.com/MadAppGang/httplog/v2@v2.0.0📚 Full Documentation
See the README for complete usage examples.