Skip to content

feat: add structured JSON logging using structlog (closes #19)#47

Merged
Teboho66 merged 2 commits into
Teboho66:mainfrom
Anamandla:feature/issue-19-structured-json-logging
Jun 8, 2026
Merged

feat: add structured JSON logging using structlog (closes #19)#47
Teboho66 merged 2 commits into
Teboho66:mainfrom
Anamandla:feature/issue-19-structured-json-logging

Conversation

@Anamandla

Copy link
Copy Markdown
Contributor

Summary

Replaces ad-hoc print() statements with structured JSON logging using structlog, making SentinelPay logs parseable by tools like Grafana Loki and AWS CloudWatch.

Changes

Assignment 12/api/logging_config.py (new file)

  • Configures structlog with ISO timestamps, log levels, exception formatting, and JSON rendering to stdout
  • Exposes a get_logger() helper for use across the codebase

Assignment 12/handlers/routes/transactions.py

  • Added logger.info("transaction_submitted", ...) on every successful transaction submission, including transaction_id, amount, and currency
  • Added logger.info("fraud_decision_applied", ...) on every fraud decision, including transaction_id and fraud_score
  • Added logger.error("unhandled_error", ...) with traceback for all unhandled 500 errors

requirements.txt

  • Added structlog dependency

Sample Log Output

{"timestamp": "2026-06-08T10:00:00Z", "level": "info", "event": "transaction_submitted", "transaction_id": "txn-abc-123", "amount": 500.0, "currency": "ZAR"}
{"timestamp": "2026-06-08T10:00:01Z", "level": "info", "event": "fraud_decision_applied", "transaction_id": "txn-abc-123", "fraud_score": 0.87}

Acceptance Criteria Met

  • Every transaction submission produces a JSON log line
  • Every fraud decision produces a JSON log line
  • Every unhandled error produces an ERROR log with traceback
  • Logs written to stdout (Docker-friendly)

Testing

Ran PYTHONPATH=".:../Assignment 10:../Assignment 11" pytest tests/ -v — all 116 tests pass.

Closes #19

@222618698 222618698 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sthe

@Teboho66
Teboho66 self-requested a review June 8, 2026 16:08

@Teboho66 Teboho66 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good

@Teboho66
Teboho66 merged commit 66e8be5 into Teboho66:main Jun 8, 2026
1 check failed
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.

Add structured JSON logging using structlog

3 participants