Skip to content

Conversation

@prosdev
Copy link
Contributor

@prosdev prosdev commented Jan 12, 2026

Closes #8

Implement structured logging with dual formatters (JSON/console) for production observability.

Changes

Infrastructure (8 commits):

  1. Configure structlog with dual formatters
  2. Add API logging with context propagation
  3. Add Processor logging
  4. Add Buffer logging with timing
  5. Add EventStore and ErrorStore logging
  6. Unit tests for logging configuration
  7. Documentation (LOCAL_DEV.md, CLAUDE.md)
  8. Fix config tests

What's Logged:

  • API requests: method, path, status_code, duration_ms
  • Events: stream, event_type (NOT full payload)
  • Buffer flushes: partition, event_count, duration_ms
  • Store writes: event_count, duration_ms
  • Errors: Validation failures, store failures

Context Propagation:

  • request_id: UUID for tracing requests
  • stream: Event stream name
  • Propagates through entire call chain

Modes:

  • Development: Colored console (human-readable)
  • Production: JSON (machine-readable)
  • Configurable via EVENTKIT_LOG_LEVEL and EVENTKIT_JSON_LOGS

Tests:

  • All 212 unit tests passing
  • Added 7 new logging configuration tests

Configuration

# Development (default)
export EVENTKIT_LOG_LEVEL=INFO
export EVENTKIT_JSON_LOGS=false

# Production
export EVENTKIT_LOG_LEVEL=INFO
export EVENTKIT_JSON_LOGS=true

Add structured logging infrastructure with JSON and console formatters.

Files Created:
- src/eventkit/logging/__init__.py
- src/eventkit/logging/config.py

Configuration:
- Dual formatters: JSON for prod, colored console for dev
- Context propagation via contextvars
- ISO timestamps, log level, logger name in all logs

Settings Added:
- EVENTKIT_LOG_LEVEL (default: INFO)
- EVENTKIT_JSON_LOGS (default: False)

Call configure_logging() in FastAPI app creation.
Add logging to API router with context propagation and timing:

Changes:
- Use structlog.get_logger() instead of stdlib logging
- Bind request context (request_id, stream) for all logs
- Log request_received with method, path, event_type
- Log request_completed with status_code, duration_ms
- Log request_failed with error, error_type, status_code
- Clear context after request (in finally block)
- Move logging configuration to lifespan (startup)

Metrics Logged:
- duration_ms: Request processing time
- event_type: Type of event received
- status_code: HTTP response status

Context Variables:
- request_id: UUID for tracing
- stream: Event stream name

All tests passing.
Add logging to event processing pipeline:

Changes:
- Log event_received (DEBUG): event_type, stream
- Log adaptation_failed (WARN): error, event_type, stream
- Log event_adapted (DEBUG): event_type
- Log event_sequenced (DEBUG): partition

Never logs full event payloads (PII safety).

All tests passing.
Add logging to buffer operations with performance metrics:

Changes:
- Log buffer_add (DEBUG): partition, buffer_size
- Log buffer_flush_start (INFO): partition, event_count
- Log buffer_flush_complete (INFO): partition, event_count, duration_ms

Performance tracking with timing measurements for flush operations.

All tests passing.
Add logging to storage operations:

EventStore (FirestoreEventStore):
- Log store_write_start (INFO): event_count
- Log store_write_complete (INFO): event_count, duration_ms, collection
- Log store_write_failed (ERROR): event_count, error, error_type, duration_ms

ErrorStore (FirestoreErrorStore):
- Log error_stored (WARN): stream, error

Never logs full event payloads (PII safety).

All tests passing.
Add comprehensive tests for structlog configuration:

Configuration Tests:
- Log level setting (DEBUG, INFO)
- JSON mode configuration
- Console mode configuration
- Logger functionality after config

Context Propagation Tests:
- Context variable binding
- Context variable clearing

All tests passing.
Update LOCAL_DEV.md and CLAUDE.md with structured logging guides:

LOCAL_DEV.md:
- Development vs production logging
- Log level configuration
- JSON logs for production
- Example output for both modes
- Log levels and what they mean
- What is/is not logged (PII safety)
- Context propagation for tracing

CLAUDE.md:
- How to use structlog correctly
- What to log at each level
- What NEVER to log (PII, tokens, etc)
- Context propagation patterns
- Performance timing examples

Complete structured logging implementation.
Rename test file and update assertions:
- tests/unit/logging/test_config.py → test_logging_config.py (avoid conflict)
- LOG_LEVEL → EVENTKIT_LOG_LEVEL
- Add EVENTKIT_JSON_LOGS assertions

All tests passing.
@prosdev prosdev merged commit 3988315 into main Jan 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Structured Logging

2 participants