Documentation
¶
Overview ¶
Package logger provides structured logging using zerolog.
This package implements production-ready logging with: - Structured JSON logging for production (machine-parsable) - Pretty console output for development (human-readable) - Component-specific loggers (security, websocket, webhook, etc.) - Configurable log levels (debug, info, warn, error, fatal)
SECURITY ENHANCEMENT (2025-11-14): Added structured logging to replace fmt.Printf and log.Printf calls. Benefits: - Machine-parsable logs for security monitoring and alerting - Consistent log format across all components - Automatic field extraction for log aggregation (e.g., Elasticsearch) - Performance: Zero-allocation JSON logging in production
Usage:
// Initialize once in main()
logger.Initialize("info", false) // production: JSON output
logger.Initialize("debug", true) // development: pretty output
// Use component-specific loggers
logger.Security().Warn().
Str("user_id", "user123").
Str("ip", "203.0.113.42").
Msg("Failed MFA attempt")
// Use global logger for general events
logger.Log.Info().Msg("Application started")
Index ¶
Constants ¶
This section is empty.
Variables ¶
var (
Log zerolog.Logger
)
Global logger instance - use this for general application logging.
For component-specific logging, use the helper functions like Security(), WebSocket(), etc. to get loggers with pre-configured component tags.
Functions ¶
func Initialize ¶
Initialize sets up the global logger with the specified level and output format.
This function should be called once at application startup before any logging occurs.
Parameters:
- level: Log level as string ("debug", "info", "warn", "error", "fatal", "panic") Defaults to "info" if invalid level provided
- pretty: If true, use human-readable console output (development) If false, use JSON output (production)
Production Configuration:
logger.Initialize("info", false)
Output: {"level":"info","service":"streamspace-api","time":1699999999,"message":"User logged in"}
Development Configuration:
logger.Initialize("debug", true)
Output: 10:30:00 INF User logged in service=streamspace-api
Log Levels (from most to least verbose):
- debug: Detailed debugging information
- info: General informational messages
- warn: Warning messages (potential issues)
- error: Error messages (handled errors)
- fatal: Fatal errors (application exits)
- panic: Panic errors (application crashes)
func Integration ¶
Integration creates a logger for integration events
Types ¶
This section is empty.