Update status of all epic0 stories (0.1-0.5) from Pending to Completed: - 0.1: Project Initialization - Directory structure and Go module setup - 0.2: Configuration Management System - Viper-based config implemented - 0.3: Structured Logging System - Zap logger with middleware implemented - 0.4: CI/CD Pipeline - GitHub Actions workflow with tests and linting - 0.5: DI and Bootstrap - FX-based DI container with lifecycle management All stories have been implemented with tests and are working.
4.7 KiB
4.7 KiB
Story 0.3: Structured Logging System
Metadata
- Story ID: 0.3
- Title: Structured Logging System
- Epic: 0 - Project Setup & Foundation
- Status: Completed
- Priority: High
- Estimated Time: 4-6 hours
- Dependencies: 0.1, 0.2
Goal
Implement a production-ready logging system with structured JSON output, request correlation, and configurable log levels that can be used by all modules.
Description
This story implements a complete logging system using Zap that provides structured logging, request correlation via request IDs, and context-aware logging. The system must support both development (human-readable) and production (JSON) formats.
Deliverables
1. Logger Interface (pkg/logger/logger.go)
Define Logger interface with:
Debug(msg string, fields ...Field)- Debug level loggingInfo(msg string, fields ...Field)- Info level loggingWarn(msg string, fields ...Field)- Warning level loggingError(msg string, fields ...Field)- Error level loggingWith(fields ...Field) Logger- Create child logger with fieldsWithContext(ctx context.Context) Logger- Create logger with context fieldsFieldtype for structured fields- Package-level convenience functions
2. Zap Implementation (internal/logger/zap_logger.go)
Implement Logger interface using Zap:
- Structured JSON logging for production mode
- Human-readable console logging for development mode
- Configurable log levels (debug, info, warn, error)
- Request-scoped fields support
- Context-aware logging (extract request ID, user ID from context)
- Field mapping to Zap fields
- Error stack trace support
3. Request ID Middleware (internal/logger/middleware.go)
Gin middleware for request correlation:
- Generate unique request ID per HTTP request
- Add request ID to request context
- Add request ID to all logs within request context
- Return request ID in response headers (
X-Request-ID) - Support for existing request IDs in headers
4. Global Logger Export (pkg/logger/global.go)
- Export default logger instance
- Package-level convenience functions
- Thread-safe logger access
5. DI Integration
- Provider function for Logger
- Register in DI container
- Make configurable via FX
Implementation Steps
-
Install Dependencies
go get go.uber.org/zap@v1.26.0 -
Create Logger Interface
- Define
Loggerinterface inpkg/logger/logger.go - Define
Fieldtype for structured fields - Add package documentation
- Define
-
Implement Zap Logger
- Create
internal/logger/zap_logger.go - Implement all interface methods
- Support both JSON and console encoders
- Handle log levels and field mapping
- Create
-
Create Request ID Middleware
- Create
internal/logger/middleware.go - Implement Gin middleware
- Generate and propagate request IDs
- Add to response headers
- Create
-
Add Global Logger
- Create
pkg/logger/global.go - Export default logger
- Add convenience functions
- Create
-
Integrate with DI
- Create provider function
- Register in DI container
- Test injection
Acceptance Criteria
Loggerinterface is defined and documented- Zap implementation supports JSON and console formats
- Log levels are configurable and respected
- Request IDs are generated and included in all logs
- Request ID middleware works with Gin
- Context-aware logging extracts request ID and user ID
- Logger can be injected via DI container
- All modules can use logger through interface
- Request correlation works across service boundaries
- Structured fields work correctly
Related ADRs
Implementation Notes
- Use Zap's production and development presets
- Request IDs should be UUIDs or similar unique identifiers
- Context should be propagated through all service calls
- Log levels should be configurable via configuration system
- Consider adding log sampling for high-volume production
- Support for log rotation and file output (future enhancement)
Testing
# Test logger interface
go test ./pkg/logger/...
# Test Zap implementation
go test ./internal/logger/...
# Test request ID middleware
go test ./internal/logger/... -run TestRequestIDMiddleware
Files to Create/Modify
pkg/logger/logger.go- Logger interfacepkg/logger/global.go- Global logger exportinternal/logger/zap_logger.go- Zap implementationinternal/logger/middleware.go- Request ID middlewareinternal/di/providers.go- Add logger providerconfig/default.yaml- Add logging configuration