How renuo/rails_api_logger works
Main Components
• Rails Application Integration – Show that this gem is integrated into a main Rails application.
• API Request Logging Middleware – Represent the Rails middleware (RailsApiLogger::Middleware) that intercepts inbound requests.
• Inbound Request Logging – Represent the models and controllers dedicated to logging details of API calls coming into the application. In the gem, this is mainly handled by models (such as InboundRequestLog) and the middleware that hooks into the request flow.
• Outbound Request Logging – Represent how outbound API calls are intercepted/logged. This includes the RailsApiLogger::Logger service class and associated OutboundRequestLog model.
• Database Components – Illustrate the two separate logging tables created by the gem (inbound_request_logs and outbound_request_logs). Also note the option to use a separate database or different connection pools.
• Rails Engine and Generators – Include the Rails engine (found in lib/rails_api_logger/engine.rb) which automatically loads the gem’s components into the Rails boot process, and the generator that helps set up migrations (and configuration).
Relationships and Data Flow
• Inbound Logging Flow:
▸ An inbound HTTP request is routed through the Rails stack.
▸ Before Rails’ own logging (Rails::Rack::Logger), the middleware (RailsApiLogger::Middleware) intercepts the request.
▸ The middleware records relevant information (e.g., path, method, body, response) and feeds it into the InboundRequestLog model for database persistence.
▸ Optionally, the application can attach domain models (using methods like attach_inbound_request_loggable) to the logged records.
• Outbound Logging Flow:
▸ When making an HTTP request outbound (using Net::HTTP), the code can either create a log using the from_request factory method or wrap the request call using RailsApiLogger::Logger.
▸ The logger captures request details, executes the HTTP call, and then logs response data to the OutboundRequestLog model.
• Integration with RailsAdmin:
▸ Optionally, the gem provides RailsAdmin integration for better management and searchability of log records.
Architectural Patterns and Design Principles
• Modularity and Separation of Concerns: The logging functionality is separated from the main app logic, encapsulated in a self-contained Rails Engine.
• Middleware Interception: Use of middleware to capture inbound API behavior before the standard Rails logger.
• Service Wrapping for Outbound Calls: Encapsulates outbound request logging in a service object (the Logger class) ensuring logs are persisted even in cases of errors or aborted transactions.
• Database Isolation Consideration: The configuration option to use a separate database connection prevents log loss due to transaction rollbacks in the main database.
Technologies and Frameworks
• Ruby on Rails – The core framework used for building and integrating the logging features.
• ActiveRecord – For defining and interacting with the logging tables.
• Rails Middleware – For intercepting request flow.
• Rails Engines and Generators – To embed gem functionality into a customer app seamlessly.
• RailsAdmin (optional) – For administration of logs in a GUI.