# renuo/rails_api_logger architecture

> Architecture diagram of the GitHub repository renuo/rails_api_logger, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/renuo/rails_api_logger
- Repository: https://github.com/renuo/rails_api_logger

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Rails Application Layer
    subgraph "Rails Application"
        A["Rails Application Integration"]
    end

    %% Engine and Setup Layer
    subgraph "Engine & Setup"
        B["Rails Engine"]
        C["Install Generator"]
        subgraph "Migrations"
            MT["Migration Template"]
            MF["Migration File"]
        end
    end

    %% Inbound Logging Module
    subgraph "Inbound Logging Module"
        M["RailsApiLogger::Middleware"]
        I1["InboundRequestLog Model"]
        I2["Inbound Logging Controller"]
    end

    %% Outbound Logging Module
    subgraph "Outbound Logging Module"
        O1["Logger Service"]
        O2["OutboundRequestLog Model"]
    end

    %% Database Layer
    subgraph "Database Layer"
        D1["Inbound_Request_Logs Table"]
        D2["Outbound_Request_Logs Table"]
        Dconfig["DB Config (Separate Connection)"]
    end

    %% Integration Flows
    A -->|"integrates_via_Engine"| B
    B -->|"mounts"| M
    B -->|"loads_migrations"| C
    C -->|"creates_template"| MT
    C -->|"installs_migration"| MF
    MT -->|"creates"| D1
    MT -->|"creates"| D2
    MF -->|"creates"| D1
    MF -->|"creates"| D2

    %% Inbound Logging Flow
    A -->|"request_inbound"| M
    M -->|"triggers_logging"| I2
    M -->|"logs_data"| I1
    I1 -->|"persists_log"| D1
    I2 -->|"persists_log"| D1

    %% Outbound Logging Flow
    A -->|"outbound_call"| O1
    O1 -->|"creates_log"| O2
    O2 -->|"persists_log"| D2

    %% Database Configuration Application
    Dconfig ---|"applies_to"| D1
    Dconfig ---|"applies_to"| D2

    %% Click Events
    click A "https://github.com/renuo/rails_api_logger/tree/main/spec/dummy"
    click M "https://github.com/renuo/rails_api_logger/blob/main/app/middlewares/rails_api_logger/middleware.rb"
    click I1 "https://github.com/renuo/rails_api_logger/blob/main/app/models/rails_api_logger/inbound_request_log.rb"
    click I2 "https://github.com/renuo/rails_api_logger/blob/main/app/controllers/inbound_requests_logger.rb"
    click O1 "https://github.com/renuo/rails_api_logger/blob/main/app/models/rails_api_logger/logger.rb"
    click O2 "https://github.com/renuo/rails_api_logger/blob/main/app/models/rails_api_logger/outbound_request_log.rb"
    click B "https://github.com/renuo/rails_api_logger/blob/main/lib/rails_api_logger/engine.rb"
    click C "https://github.com/renuo/rails_api_logger/blob/main/lib/generators/rails_api_logger/install_generator.rb"
    click MT "https://github.com/renuo/rails_api_logger/blob/main/lib/generators/templates/create_rails_api_logger_table.rb.tt"
    click MF "https://github.com/renuo/rails_api_logger/blob/main/spec/dummy/db/api_logger_migrate/20241118201923_create_rails_api_logger_table.rb"

    %% Styles
    class A rails
    class B engine
    class C engine
    class M middleware
    class I1 mod
    class I2 mod
    class O1 mod
    class O2 mod
    class D1 database
    class D2 database
    class Dconfig database

    %% Class Definitions
    classDef rails fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef middleware fill:#e6ffe6,stroke:#0a0,stroke-width:2px;
    classDef mod fill:#fff2e6,stroke:#cc6600,stroke-width:2px;
    classDef engine fill:#e6f0ff,stroke:#1a75ff,stroke-width:2px;
    classDef database fill:#ffe6e6,stroke:#cc0000,stroke-width:2px;
```
