Documentation
¶
Overview ¶
Package bolt provides a high-performance, zero-allocation structured logging library for Go.
Bolt is designed for applications that demand exceptional performance without compromising on features. It delivers sub-100ns logging operations with zero memory allocations in hot paths.
Key Features ¶
- Zero allocations in hot paths - Sub-100ns latency for logging operations - Type-safe field methods - JSON and console output formats - OpenTelemetry tracing integration - Environment variable configuration - Production-ready reliability
Performance ¶
Bolt achieves industry-leading performance:
- 105.2ns/op with 0 allocations
- 64% faster than Zerolog
- 80% faster than Zap
- 2603% faster than Logrus
Quick Start ¶
Basic usage with JSON output:
package main
import (
"os"
"github.com/felixgeelhaar/bolt"
)
func main() {
logger := bolt.New(bolt.NewJSONHandler(os.Stdout))
logger.Info().
Str("service", "auth").
Int("user_id", 12345).
Msg("User authenticated")
}
Console output with colors:
logger := bolt.New(bolt.NewConsoleHandler(os.Stdout))
logger.Info().Str("status", "ready").Msg("Server started")
Configuration ¶
Environment variables:
- BOLT_LEVEL: Set log level (trace, debug, info, warn, error, fatal)
- BOLT_FORMAT: Set output format (json, console)
Programmatic configuration:
logger := bolt.New(bolt.NewJSONHandler(os.Stdout)).SetLevel(bolt.DEBUG)
Zero Allocations ¶
Bolt uses object pooling and direct serialization to achieve zero allocations:
// This logging operation performs 0 allocations
logger.Info().
Str("method", "GET").
Str("path", "/api/users").
Int("status", 200).
Dur("duration", time.Since(start)).
Msg("Request completed")
OpenTelemetry Integration ¶
Bolt automatically extracts trace information from context:
ctx := context.Background()
logger.Info().Ctx(ctx).Msg("Operation completed")
Thread Safety ¶
All Bolt operations are thread-safe and can be used concurrently across goroutines. The library uses atomic operations for level changes and sync.Pool for event management.
Security Features ¶
Bolt includes comprehensive security protections:
- Automatic JSON escaping prevents log injection attacks
- Input validation with configurable size limits (keys: 256 chars, values: 64KB)
- Control character filtering in keys
- Buffer size limits prevent resource exhaustion (max 1MB per entry)
- Thread-safe operations prevent race conditions
- Secure error handling prevents information disclosure
Performance Characteristics ¶
Bolt delivers industry-leading performance:
- Zero allocations in hot paths through intelligent event pooling
- Sub-100ns latency for most logging operations
- Custom serialization optimized for common data types
- Lock-free event management with atomic synchronization
Index ¶
- Constants
- type ConsoleHandler
- type ErrorHandler
- type Event
- func (e *Event) Any(key string, value interface{}) *Event
- func (e *Event) Base64(key string, value []byte) *Event
- func (e *Event) Bool(key string, value bool) *Event
- func (e *Event) Bytes(key string, value []byte) *Event
- func (e *Event) Caller() *Event
- func (e *Event) Counter(key string, counter *int64) *Event
- func (e *Event) Dur(key string, value time.Duration) *Event
- func (e *Event) Err(err error) *Event
- func (e *Event) Fields(fields map[string]interface{}) *Event
- func (e *Event) Float64(key string, value float64) *Event
- func (e *Event) Hex(key string, value []byte) *Event
- func (e *Event) Int(key string, value int) *Event
- func (e *Event) Int64(key string, value int64) *Event
- func (e *Event) Interface(key string, value interface{}) *Event
- func (e *Event) Logger() *Logger
- func (e *Event) Msg(message string)
- func (e *Event) Printf(format string, args ...interface{})
- func (e *Event) RandID(key string) *Event
- func (e *Event) Send()
- func (e *Event) Stack() *Event
- func (e *Event) Str(key, value string) *Event
- func (e *Event) Time(key string, value time.Time) *Event
- func (e *Event) Timestamp() *Event
- func (e *Event) Uint(key string, value uint) *Event
- func (e *Event) Uint64(key string, value uint64) *Event
- type Handler
- type JSONHandler
- type Level
- type Logger
- func (l *Logger) Ctx(ctx context.Context) *Logger
- func (l *Logger) Debug() *Event
- func (l *Logger) Error() *Event
- func (l *Logger) Fatal() *Event
- func (l *Logger) Info() *Event
- func (l *Logger) SetErrorHandler(eh ErrorHandler) *Logger
- func (l *Logger) SetLevel(level Level) *Logger
- func (l *Logger) Trace() *Event
- func (l *Logger) Warn() *Event
- func (l *Logger) With() *Event
Constants ¶
const ( // DefaultBufferSize is the initial buffer size for events - increased to reduce reallocations DefaultBufferSize = 2048 // MaxBufferSize is the maximum allowed buffer size to prevent unbounded growth MaxBufferSize = 1024 * 1024 // 1MB // StackTraceBufferSize is the buffer size for stack traces StackTraceBufferSize = 64 * 1024 // 64KB // DefaultFilePermissions for log files DefaultFilePermissions = 0644 // MaxKeyLength is the maximum allowed key length MaxKeyLength = 256 // MaxValueLength is the maximum allowed value length MaxValueLength = 64 * 1024 // 64KB )
Constants for buffer sizes and configuration
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConsoleHandler ¶
type ConsoleHandler struct {
// contains filtered or unexported fields
}
ConsoleHandler formats logs for human-readable console output.
func NewConsoleHandler ¶
func NewConsoleHandler(out io.Writer) *ConsoleHandler
NewConsoleHandler creates a new ConsoleHandler.
func (*ConsoleHandler) Write ¶
func (h *ConsoleHandler) Write(e *Event) error
Write handles the log event.
type ErrorHandler ¶
type ErrorHandler func(err error)
ErrorHandler is called when a handler write operation fails
type Event ¶
type Event struct {
// contains filtered or unexported fields
}
Event represents a single log message - now the primary type to eliminate wrapper allocation
func Debug ¶
func Debug() *Event
Debug starts a new message with the DEBUG level on the default logger.
func Error ¶
func Error() *Event
Error starts a new message with the ERROR level on the default logger.
func Fatal ¶
func Fatal() *Event
Fatal starts a new message with the FATAL level on the default logger.
func Trace ¶
func Trace() *Event
Trace starts a new message with the TRACE level on the default logger.
func (*Event) Msg ¶
Msg sends the event to the handler for processing. This is always the final method in the chain.
func (*Event) Send ¶
func (e *Event) Send()
Send is an alias for Msg for consistency with other logging libraries.
type Handler ¶
type Handler interface {
// Write handles the log event, writing it to its destination.
// The handler is responsible for returning the event's buffer to the pool.
Write(e *Event) error
}
Handler processes a log event and writes it to an output.
type JSONHandler ¶
type JSONHandler struct {
// contains filtered or unexported fields
}
JSONHandler formats logs as JSON.
func NewJSONHandler ¶
func NewJSONHandler(out io.Writer) *JSONHandler
NewJSONHandler creates a new JSON handler.
func (*JSONHandler) Write ¶
func (h *JSONHandler) Write(e *Event) error
Write handles the log event.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is the main logging interface.
func (*Logger) SetErrorHandler ¶
func (l *Logger) SetErrorHandler(eh ErrorHandler) *Logger
SetErrorHandler sets a custom error handler for the logger
func (*Logger) SetLevel ¶
SetLevel sets the logging level for the logger using atomic operations for thread safety. This method is safe to call concurrently from multiple goroutines without additional synchronization. The atomic operations prevent race conditions that could lead to inconsistent filtering behavior or security bypass scenarios.
Directories
¶
| Path | Synopsis |
|---|---|
|
benchmark
|
|
|
alerting
Package alerting provides comprehensive alerting system for performance monitoring
|
Package alerting provides comprehensive alerting system for performance monitoring |
|
cmd/bolt-benchmark
command
Package main provides a comprehensive command-line interface for Bolt benchmarking
|
Package main provides a comprehensive command-line interface for Bolt benchmarking |
|
enterprise
Package enterprise provides comprehensive benchmarking for real-world enterprise scenarios
|
Package enterprise provides comprehensive benchmarking for real-world enterprise scenarios |
|
framework
Package framework provides comprehensive benchmarking tools for competitive analysis of logging libraries with statistical significance and enterprise-grade validation.
|
Package framework provides comprehensive benchmarking tools for competitive analysis of logging libraries with statistical significance and enterprise-grade validation. |
|
validation
Package validation provides comprehensive performance validation and regression detection
|
Package validation provides comprehensive performance validation and regression detection |
|
examples
|
|
|
cloud-native/kubernetes/app
command
Package main implements a Kubernetes-ready application with Bolt logging.
|
Package main implements a Kubernetes-ready application with Bolt logging. |
|
high-availability/load-balancer
command
Package main demonstrates load balancer integration with Bolt logging.
|
Package main demonstrates load balancer integration with Bolt logging. |
|
observability/prometheus
command
Package main demonstrates Prometheus metrics integration with Bolt logging.
|
Package main demonstrates Prometheus metrics integration with Bolt logging. |
|
security/audit-logging
command
Package main demonstrates enterprise audit logging with Bolt.
|
Package main demonstrates enterprise audit logging with Bolt. |
|
security/pii-masking
command
Package main demonstrates PII masking and data protection with Bolt logging.
|
Package main demonstrates PII masking and data protection with Bolt logging. |
|
migrate
|
|
|
common
Package common provides shared utilities for all migration tools.
|
Package common provides shared utilities for all migration tools. |
|
logrus
Package logrus provides benchmarking tools for comparing Logrus and Bolt performance.
|
Package logrus provides benchmarking tools for comparing Logrus and Bolt performance. |
|
stdlog
Package stdlog provides benchmarking tools for comparing standard log and Bolt performance.
|
Package stdlog provides benchmarking tools for comparing standard log and Bolt performance. |
|
zap
Package zap provides compatibility layer and migration tools for Zap users.
|
Package zap provides compatibility layer and migration tools for Zap users. |
|
zerolog
Package zerolog provides a compatibility adapter for migrating from rs/zerolog to Bolt
|
Package zerolog provides a compatibility adapter for migrating from rs/zerolog to Bolt |
|
monitoring
|
|
|
jaeger
command
|