task/1.12
Raw Download raw file

Task 1.12: Structured Logging

Summary

Implement structured logging system for buylater.email with different log levels, proper formatting, and configurable output to improve debugging and operational monitoring.

Motivation

Following Let’s Go Chapter 3.2, we need to replace basic log.Println() calls with a structured logging system that provides better visibility into application behavior and makes debugging easier in production.

Acceptance Criteria

  • Create separate loggers for different types of messages (info, error)
  • Implement structured log message formatting with timestamps
  • Add log levels (INFO, ERROR, WARN) with proper filtering
  • Configure log output destinations (stdout, stderr, files)
  • Add contextual logging throughout the application
  • Implement request logging for HTTP requests
  • Add configuration for log level and output format
  • Update all existing logging to use structured format
  • Add operational logging for key application events

Technical Requirements

Logger Structure

type application struct {
    config config
    logger struct {
        info  *log.Logger
        error *log.Logger
    }
    serviceName string
}

Implementation Details

  • Use Go’s standard log package with custom configuration
  • Create separate loggers for different message types
  • Implement consistent log message formatting
  • Add request ID tracking for better traceability
  • Include relevant context in all log messages
  • Configure log prefixes and formatting flags

Dependencies

  • Task 1.11 completed (configuration management foundation)

Testing Strategy

Unit Tests

  • Test logger initialization and configuration
  • Verify log message formatting and output
  • Test different log levels and filtering

Integration Tests

  • Log messages appear in correct output streams
  • Request logging captures all HTTP requests
  • Error logging works for all error conditions

Manual Testing

  • Start application and verify startup logging
  • Make HTTP requests and check request logs
  • Trigger errors and verify error logging format
  • Test different log level configurations
  • Verify log output goes to correct destinations

Definition of Done

  • All acceptance criteria met
  • All specified tests pass
  • Code follows project conventions (go fmt, go vet)
  • Logging system is consistent throughout application
  • Log messages are informative and properly formatted
  • No remaining uses of basic log.Println()
  • Human verification completed successfully
  • Git commit created with proper message format

Implementation Notes

Approach

Replace ad-hoc logging with a structured system that provides clear, consistent, and useful log messages for both development and production operations.

Key Files to Modify

  • cmd/web/main.go - Initialize structured loggers
  • cmd/web/handlers.go - Update all logging calls
  • Add request logging middleware (prepare for future middleware)

Log Message Categories

  • INFO: Application startup, configuration, normal operations
  • ERROR: Application errors, HTTP errors, template errors
  • REQUEST: HTTP request details (method, path, response code, duration)

Log Message Format

2025/07/24 10:30:45 INFO  Starting server on http://localhost:4000
2025/07/24 10:30:46 ERROR Template parsing failed: template not found
2025/07/24 10:30:47 INFO  Request: GET / -> 200 OK (15ms)

Potential Risks

  • Log volume could impact performance
  • Log formatting changes might break log parsing tools
  • Missing error context could reduce debugging effectiveness

Success Metrics

Professional logging system that provides clear visibility into buylater.email application behavior with structured, consistent, and useful log messages for operations and debugging.

  • Blocks: Future monitoring, alerting, and operational tools
  • Blocked by: Task 3.1 (needs configuration system)
  • Related: Error handling, monitoring, and debugging

Implementation Log

Implementation details will be recorded here during development.


Final Verification

Human Tester: [Name]
Date Completed: [YYYY-MM-DD]
Verification Result: [Pass/Fail]
Notes: [Any issues found or additional observations]