Structured Logging
Learn how to implement structured logging with Rivaas using Go’s standard log/slog
2 minute read
The Rivaas Logging package provides production-ready structured logging with minimal dependencies. Uses Go’s built-in log/slog for high performance and native integration with the Go ecosystem.
Features
- Multiple Output Formats: JSON, text, and human-friendly console output
- Context-Aware Logging: Automatic trace correlation with OpenTelemetry
- Sensitive Data Redaction: Automatic sanitization of passwords, tokens, and secrets
- Log Sampling: Reduce log volume in high-traffic scenarios
- Convenience Methods: HTTP request logging, error logging with context, duration tracking
- Dynamic Log Levels: Change log levels at runtime without restart
- Functional Options API: Clean, composable configuration
- Router Integration: Seamless integration following metrics/tracing patterns
- Zero External Dependencies: Uses only Go standard library (except OpenTelemetry for trace correlation)
Quick Start
package main
import (
"rivaas.dev/logging"
)
func main() {
// Create a logger with console output
log := logging.MustNew(
logging.WithConsoleHandler(),
logging.WithDebugLevel(),
)
log.Info("service started", "port", 8080, "env", "production")
log.Debug("debugging information", "key", "value")
log.Error("operation failed", "error", "connection timeout")
}package main
import (
"rivaas.dev/logging"
)
func main() {
// Create a logger with JSON output
log := logging.MustNew(
logging.WithJSONHandler(),
logging.WithServiceName("my-api"),
logging.WithServiceVersion("v1.0.0"),
logging.WithEnvironment("production"),
)
log.Info("user action", "user_id", "123", "action", "login")
// Output: {"time":"2024-01-15T10:30:45.123Z","level":"INFO","msg":"user action","service":"my-api","version":"v1.0.0","env":"production","user_id":"123","action":"login"}
}package main
import (
"rivaas.dev/logging"
)
func main() {
// Create a logger with text output
log := logging.MustNew(
logging.WithTextHandler(),
logging.WithServiceName("my-api"),
)
log.Info("service started", "port", 8080)
// Output: time=2024-01-15T10:30:45.123Z level=INFO msg="service started" service=my-api port=8080
}How It Works
- Handler types determine output format (JSON, Text, Console)
- Structured fields are key-value pairs, not string concatenation
- Log levels control verbosity (Debug, Info, Warn, Error)
- Service metadata automatically added to every log entry
- Sensitive data automatically redacted (passwords, tokens, keys)
Learning Path
Follow these guides to master logging with Rivaas:
- Installation - Get started with the logging package
- Basic Usage - Learn handler types and output formats
- Configuration - Configure loggers with all available options
- Context Logging - Add trace correlation with OpenTelemetry
- Convenience Methods - Use helper methods for common patterns
- Log Sampling - Reduce log volume in high-traffic scenarios
- Dynamic Log Levels - Change log levels at runtime
- Router Integration - Integrate with Rivaas router
- Testing - Test utilities and patterns
- Best Practices - Performance tips and patterns
- Migration - Switch from other logging libraries
- Examples - See real-world usage patterns
Next Steps
- Start with Installation to set up the logging package
- Explore the API Reference for complete technical details
- Check out code examples on GitHub
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.