Skip to content

feat: add HTTP request/response body logging with RequestLogger trait#49

Closed
ammario wants to merge 1 commit intomainfrom
body-logging
Closed

feat: add HTTP request/response body logging with RequestLogger trait#49
ammario wants to merge 1 commit intomainfrom
body-logging

Conversation

@ammario
Copy link
Copy Markdown
Member

@ammario ammario commented Sep 14, 2025

Summary

  • Implement comprehensive HTTP request/response body logging functionality
  • Add RequestLogger trait for cleaner, more extensible architecture
  • Support streaming body logging with zero performance impact when disabled

Key Changes

New Architecture

  • RequestLogger trait: Clean abstraction with FileRequestLogger and NoopLogger implementations
  • Cleaner API: RuleEngine now accepts Arc instead of Option<Arc<Mutex>>

Body Logging Features

  • Environment-controlled: Enabled via HTTPJAIL_REQUEST_LOG_BODY environment variable
  • Streaming implementation: Bodies logged as they stream through, avoiding memory overhead
  • Request correlation: 4-character hex IDs correlate request/response pairs
  • Binary data handling: Automatic base64 encoding for non-UTF8 data
  • Truncation: Large bodies truncated at 10KB with clear indication

Log Format

2024-01-15T10:23:45.123Z --> 3e4f + GET https://api.example.com/users
2024-01-15T10:23:45.124Z --> 3e4f:REQ {"user": "alice"}
2024-01-15T10:23:45.234Z <-- 3e4f 200 OK
2024-01-15T10:23:45.235Z <-- 3e4f:BODY {"id": 123, "name": "Alice"}

Test Plan

  • All existing tests pass
  • New integration tests for body logging functionality
  • Tests for NoopLogger behavior
  • Tests for request ID generation and uniqueness
  • Tests for both allowed and denied requests

🤖 Generated with Claude Code

Implement comprehensive body logging functionality that logs HTTP request and
response bodies when HTTPJAIL_REQUEST_LOG_BODY environment variable is set.

Changes:
- Add RequestLogger trait with FileRequestLogger and NoopLogger implementations
- Move request logging logic from rules.rs to new request_log.rs module
- Create body_logger.rs module with streaming body wrappers
- Update RuleEngine to accept RequestLogger trait instead of Option<File>
- Add request ID generation and correlation for request/response pairs
- Support both text and binary body logging (with base64 encoding)
- Zero performance impact when body logging is disabled

The new architecture provides cleaner separation of concerns and makes the
API more extensible for future logging backends.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@ammario ammario closed this Sep 20, 2025
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.

1 participant