Skip to content

Add request logging with structured JSON output #17

Description

@AnkanMisra

Summary

Add structured JSON logging for all API requests to improve debugging and monitoring in production.

Problem

Currently, the Gateway only logs basic startup messages. There's no visibility into:

  • Incoming requests (method, path, IP)
  • Response status codes and latency
  • Payment verification outcomes
  • Error details

Proposed Solution

Add middleware that logs every request in structured JSON format:

{
  "timestamp": "2024-01-01T12:00:00Z",
  "method": "POST",
  "path": "/api/ai/summarize",
  "status": 200,
  "latency_ms": 45,
  "client_ip": "192.168.1.1",
  "payment_verified": true,
  "user_wallet": "0x123..."
}

Architecture

flowchart LR
    REQ[Incoming Request] --> LOG[Logging Middleware]
    LOG --> HANDLER[Route Handler]
    HANDLER --> LOG
    LOG --> RES[Response + Log Entry]
    
    LOG --> STDOUT[(stdout / JSON)]
Loading
sequenceDiagram
    participant C as Client
    participant L as Logger Middleware
    participant H as Handler
    
    C->>L: Request
    L->>L: Record start time
    L->>H: Forward request
    H-->>L: Response
    L->>L: Calculate latency
    L->>L: Write JSON log
    L-->>C: Response
Loading

Acceptance Criteria

  • Add logging middleware to Gateway
  • Log request method, path, status, and latency
  • Log payment verification status (402/403/200)
  • Include client IP address (handle X-Forwarded-For)
  • Use JSON format for easy parsing by log aggregators
  • Add log level configuration via environment variable
  • Don't log sensitive data (signatures, API keys)
  • Include unit tests for the logging middleware

Technical Notes

  • Gin has built-in gin.Logger() middleware as reference
  • Consider using zerolog or zap for structured logging
  • Log to stdout for container compatibility

Testing

# Run gateway and observe logs
go run main.go

# Make request and verify log output
curl -X POST http://localhost:3000/api/ai/summarize \
  -H "Content-Type: application/json" \
  -d '{"text": "test"}'

# Expected log entry in stdout

Metadata

Metadata

Assignees

Labels

SWoC26level:beginnerSmall, well-scoped work suitable for newer contributors.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions