DEV Community

Syahrul Al-Rasyid
Syahrul Al-Rasyid

Posted on

FastRTC Logging Implementation

This document describes the comprehensive logging system implemented for FastRTC services, including detailed request/response tracking for both the gRPC OCR service and CaloreportService.

Overview

The logging implementation provides:

  • Request tracking with unique IDs for correlation
  • Detailed error logging with context and retry information
  • Performance metrics for request/response timing
  • Configurable log levels and output formats
  • Service-specific loggers for fine-grained control

Services Covered

1. gRPC OCR Service Client (src/ocr/grpc_client.py)

  • Connection establishment and health checks
  • Request encoding and transmission
  • Response parsing and validation
  • Retry logic and error handling
  • Performance timing

2. CaloreportService Client (src/report_svc/client.py)

  • HTTP request/response cycles
  • JSON parsing and validation
  • Connection errors and timeouts
  • Retry attempts and backoff
  • Response data analysis

Log Message Format

Request Tracking

All requests are assigned a unique 8-character ID for correlation:

[a1b2c3d4] πŸ” Starting OCR request to localhost:50051
[a1b2c3d4] πŸ“€ Attempt 1/3 - Encoding image data
[a1b2c3d4] βœ… OCR completed in 0.234s: engine=easyocr, confidence=0.87
Enter fullscreen mode Exit fullscreen mode

Log Levels Used

INFO Level

  • Service initialization
  • Request start/completion
  • Successful operations
  • Retry attempts

DEBUG Level

  • Request parameters and configuration
  • Encoding/decoding steps
  • Response parsing details
  • Performance metrics

ERROR Level

  • Connection failures
  • Request timeouts
  • Parsing errors
  • Retry exhaustion

WARNING Level

  • Service degradation
  • Retry-able errors
  • Configuration issues

Example Log Output

gRPC OCR Service

2025-08-28 02:38:56 | src.ocr.grpc_client | INFO | extract_text_from_region:123 | [d6b4c25b] πŸ” Starting OCR request to localhost:50051
2025-08-28 02:38:56 | src.ocr.grpc_client | DEBUG | extract_text_from_region:124 | [d6b4c25b] Request params: bbox=[0, 0, 50, 50], class_name=None, engine=easyocr, languages=['en'], gpu=True
2025-08-28 02:38:56 | src.ocr.grpc_client | DEBUG | extract_text_from_region:131 | [d6b4c25b] πŸ“€ Attempt 1/3 - Encoding image data
2025-08-28 02:38:56 | src.ocr.grpc_client | DEBUG | extract_text_from_region:137 | [d6b4c25b] βœ… Image encoded successfully: 2048 bytes
2025-08-28 02:38:56 | src.ocr.grpc_client | DEBUG | extract_text_from_region:148 | [d6b4c25b] πŸ“‘ Sending gRPC request (timeout=30s)
2025-08-28 02:38:56 | src.ocr.grpc_client | ERROR | extract_text_from_region:170 | [d6b4c25b] πŸ”΄ gRPC error on attempt 1/3: code=UNAVAILABLE, details=failed to connect
Enter fullscreen mode Exit fullscreen mode

CaloreportService

2025-08-28 02:38:56 | src.report_svc.client | INFO | get_delivery_skus:89 | [24f8f831] πŸ“¦ Starting CaloreportService request for delivery_id: test-delivery-id
2025-08-28 02:38:56 | src.report_svc.client | DEBUG | get_delivery_skus:103 | [24f8f831] 🌐 Request details: URL=http://localhost:5270/api/Delivery/test-delivery-id/skus, timeout=30s, max_retries=2
2025-08-28 02:38:56 | src.report_svc.client | DEBUG | get_delivery_skus:111 | [24f8f831] πŸ“‘ Attempt 1/3 - Sending HTTP GET request
2025-08-28 02:38:56 | src.report_svc.client | DEBUG | get_delivery_skus:119 | [24f8f831] πŸ“₯ Response received in 0.145s: status=200
2025-08-28 02:38:56 | src.report_svc.client | INFO | get_delivery_skus:137 | [24f8f831] βœ… CaloreportService request successful in 0.145s: invoice=INV-001, skus=5, delivery_id=test-delivery-id
Enter fullscreen mode Exit fullscreen mode

Configuration

Environment Variables

Configure logging behavior using environment variables:

# Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
export FASTRTC_LOG_LEVEL=DEBUG

# Optional log file path
export FASTRTC_LOG_FILE=/var/log/fastrtc/app.log

# Enable/disable console logging (true/false)
export FASTRTC_LOG_CONSOLE=true

# Log format style (detailed/simple)
export FASTRTC_LOG_FORMAT=detailed
Enter fullscreen mode Exit fullscreen mode

Programmatic Configuration

from src.utils.logger import setup_fastrtc_logging

# Setup with custom configuration
setup_fastrtc_logging(
    level="DEBUG",
    log_file="/tmp/fastrtc.log",
    enable_console=True,
    format_style="detailed"
)

# Or use environment variables
from src.utils.logger import setup_logging_from_env
setup_logging_from_env()
Enter fullscreen mode Exit fullscreen mode

Logger Hierarchy

The logging system uses a hierarchical structure:

root
β”œβ”€β”€ src.ocr.grpc_client          (DEBUG level)
β”œβ”€β”€ src.report_svc.client        (DEBUG level)
β”œβ”€β”€ src.core.detector            (INFO level)
└── src.ui                       (INFO level)
Enter fullscreen mode Exit fullscreen mode

Emoji Guide

The logging uses emoji for quick visual identification:

Emoji Meaning
πŸ” Request started
πŸ“€ Data encoding/preparation
πŸ“‘ Network transmission
πŸ“₯ Response received
βœ… Successful operation
πŸ”„ Retry attempt
πŸ”΄ Error occurred
❌ Critical failure
⚠️ Warning condition
πŸ“¦ Service request
🌐 Network operation
πŸ“‹ Configuration/metadata
πŸ“Š Statistics/metrics
🚫 Resource not found
πŸ“ Location/tracking info

Performance Monitoring

Logs include timing information for performance analysis:

  • Request duration: Time from start to completion
  • Network latency: Time spent in network communication
  • Processing time: Time spent in data processing
  • Retry delays: Time spent waiting between retries

Error Analysis

Common Error Patterns

  1. Connection Refused - Service not running
  2. Timeout - Service overloaded or network issues
  3. 404 Not Found - Resource doesn't exist
  4. JSON Parse Error - Invalid response format
  5. gRPC UNAVAILABLE - OCR service down

Debugging Tips

  1. Search for request ID to trace entire request lifecycle
  2. Check INFO level for high-level operation status
  3. Use DEBUG level for detailed parameter analysis
  4. Monitor retry patterns for service health
  5. Analyze timing data for performance optimization

Integration with FastRTC

The logging system is automatically initialized when using the FastRTC application. To enable detailed logging:

# Enable debug logging
export FASTRTC_LOG_LEVEL=DEBUG

# Run application
make run ENV=dev
Enter fullscreen mode Exit fullscreen mode

Log Rotation and Management

For production deployment:

  1. Configure log file rotation using logrotate
  2. Monitor disk space usage
  3. Set up log aggregation if running multiple instances
  4. Consider structured logging formats for automated analysis

Best Practices

  1. Use appropriate log levels - DEBUG for development, INFO for production
  2. Include context - Request IDs help correlate related log entries
  3. Log performance metrics - Help identify bottlenecks
  4. Sanitize sensitive data - Don't log API keys or personal information
  5. Monitor log volume - High DEBUG logging can impact performance

Troubleshooting

High Log Volume

  • Reduce log level from DEBUG to INFO
  • Filter specific loggers if needed
  • Use log sampling for high-frequency operations

Missing Logs

  • Check logger configuration
  • Verify log level settings
  • Ensure handlers are properly configured

Performance Impact

  • Monitor logging overhead in production
  • Consider asynchronous logging for high-throughput scenarios
  • Balance between detail and performance

Top comments (0)