LogBench Documentation

repository·main·Indexed 19 days ago

https://github.com/silva96/log_bench

A Terminal User Interface (TUI) for real-time analysis of Rails application logs. LogBench provides request correlation, performance insights, and advanced filtering for HTTP requests and SQL queries. It integrates with lograge to require JSON formatted logs and offers features like request ID tracking, background job context for ActiveJob and Sidekiq, and a dedicated CLI for log file analysis.

Tokens
6.8K
Snippets
22
Records
33
Agent score
68%

What's inside LogBench

  1. Filter logs in the TUI

    main

    Press f to open the filter dialog.

    Left Pane (Request Filtering): Filter the list of HTTP requests by:

    • Method: e.g., GET, POST
    • Path: URL path patterns, e.g., /api/users
    • Status: HTTP status codes, e.g., 500
    • Controller: e.g., UsersController
    • Action: e.g., create
    • Request ID: Unique identifier, e.g., abcdef-b1n2mk ...

    Right Pane (Log Line Filtering): Filter related log lines by text content to find specific SQL queries or other log entries.

  2. Install LogBench in a Rails application

    main

    To use LogBench, add it to your Rails application's Gemfile within the development group, then run bundle install.

    LogBench is automatically enabled in the development environment and automatically configures lograge with a JSON formatter, the LogBench::JsonFormatter for the Rails logger, and LogBench::Current for request ID tracking.

    # Gemfile
    group :development do
      gem 'log_bench'
    end
    bundle install
  3. Log background jobs with context

    main

    LogBench provides enhanced logging for background jobs with colored prefixes (e.g., [JobClass#job-id]) in the TUI.

    ActiveJob

    To ensure logs receive the proper job context and prefix, use the job's logger method instead of Rails.logger.

    class EmailDeliveryJob < ApplicationJob
      def perform(user_id)
        # ✅ Correct: uses job's logger
        logger.info "Starting email delivery for user #{user_id}"
    
        # ❌ Incorrect: won't have job prefix
        Rails.logger.info "This won't have job prefix"
      end
    end

    Sidekiq

    For plain Sidekiq jobs, LogBench automatically captures context via middleware, so you can continue using Rails.logger.

    class EmailDeliveryJob < ApplicationJob
      def perform(user_id)
        logger.info "Starting email delivery for user #{user_id}"  # ✅ Will have job prefix
    
        user = User.find(user_id)
        logger.info "Found user: #{user.email}"                    # ✅ Will have job prefix
    
        # SQL queries are automatically tagged
        user.update!(last_email_sent_at: Time.current)            # ✅ Will have job prefix
    
        logger.info "Email delivery completed"                  # ✅ Will have job prefix
      end
    end
  4. Configure Lograge manually

    main

    If you prefer to manage your own lograge configuration, set config.configure_lograge_automatically = false in the LogBench initializer. You must then ensure lograge is enabled and configured to use a JSON formatter so LogBench can parse the logs.

    # config/initializers/log_bench.rb
    if defined?(LogBench)
      LogBench.setup do |config|
        config.configure_lograge_automatically = false
      end
    end
    
    # Then configure lograge in config/environments/development.rb or an initializer
    Rails.application.configure do
      config.lograge.enabled = true
      config.lograge.formatter = Lograge::Formatters::Json.new
      config.lograge.custom_options = lambda do |event|
        params = event.payload[:params]&.except("controller", "action")
        { params: params } if params.present?
      end
    end
  5. Copy content from the TUI

    main

    LogBench supports several ways to copy data to your clipboard:

    1. Smart Copy (y key):
      • In the left pane, copies complete request details (method, path, status, duration, etc.).
      • In the right pane, copies the selected SQL query along with its call source location.
    2. Text Selection Mode (t key): Toggles a mode that allows you to use your mouse to select and copy text normally. When disabled, mouse clicks are used for interface navigation.

    Clipboard Fallback:

    • macOS: Uses pbcopy.
    • Linux: Requires xclip or xsel installed via your package manager.
    • Fallback: If no tool is found, content is saved to /tmp/logbench_copy.txt.
  6. Configure LogBench via initializer

    main

    You can customize LogBench behavior by creating config/initializers/log_bench.rb. Use the LogBench.setup block to adjust settings such as enabling/disabling the gem, controlling the initialization message, or specifying which controller classes should receive request_id injection.

    Available configuration options:

    • config.enabled: Boolean. Enables or disables LogBench (defaults to true in development).
    • config.configure_lograge_automatically: Boolean. If set to false, LogBench will not attempt to configure lograge for you (defaults to true).
    • config.show_init_message: :full, :min, or :none. Controls the verbosity of the startup message (defaults to :full).
    • config.base_controller_classes: Array of strings. Specifies which controller classes to inject request_id tracking into (defaults to ['ApplicationController', 'ActionController::Base']).
    # config/initializers/log_bench.rb
    if defined?(LogBench)
      LogBench.setup do |config|
        # Enable/disable LogBench (default: true in development, false elsewhere)
        config.enabled = Rails.env.development?
      
        # Disable automatic lograge configuration (if you want to configure lograge manually)
        # config.configure_lograge_automatically = false  # (default: true)
    
        # Customize initialization message
        # config.show_init_message = :min # :full, :min, or :none (default: :full)
      
        # Specify which controllers to inject request_id tracking
        # config.base_controller_classes = %w[CustomBaseController] # (default: %w[ApplicationController ActionController::Base])
      end
    end
  7. How LogBench::JsonFormatter retrieves context attributes

    main

    To enrich log entries with metadata like request_id or job information, LogBench::JsonFormatter searches for attributes across several storage mechanisms in a specific order of precedence:

    1. LogBench::Current: The preferred mechanism. The formatter checks if LogBench::Current is defined and responds to the requested attribute.
    2. Current: A fallback for applications that define their own Current attribute class (common in Rails apps).
    3. RequestStore: If the request_store gem is present, it attempts to read from RequestStore.
    4. Thread.current: The last resort, checking thread-local storage directly.

    This allows the formatter to work seamlessly across different Rails architectures and background job processors (like Sidekiq or ActiveJob) without requiring manual configuration for every attribute.

  8. Setup LogBench in a Rails application

    main

    LogBench is designed for Rails applications using Lograge.

    1. Automatic Setup: In development environments, LogBench is automatically enabled upon installation. Simply restart your Rails server after adding the gem.
    2. Requirements: For the TUI to parse logs correctly, you must have Lograge configured to output logs in JSON format.
    3. Troubleshooting: If you encounter a "Log file not found" error, ensure you are in the Rails application root and that the target log file exists and contains content.
  9. Troubleshoot LogBench issues

    main

    No requests found

    1. Verify the log file path is correct.
    2. Ensure lograge is enabled and configured.
    3. Confirm logs are in JSON format.
    4. Generate new requests in your Rails app.

    SQL queries not showing

    1. Ensure SQL queries and HTTP requests share the same request_id.
    2. Verify Current.request_id is being set correctly.
    3. Ensure JsonFormatter is configured for your Rails logger.

    Performance issues

    1. Large log files: LogBench loads the entire file into memory. Rotate logs more frequently if files are massive.
    2. Real-time parsing: Use auto-scroll mode (a) for better performance with actively growing files.
  10. Configure LogBench automatic Lograge setup

    main

    By default, LogBench automatically configures lograge to use a JSON formatter and includes request parameters (excluding controller and action) in the log payload. If you prefer to manage your lograge configuration manually, you can disable this automatic behavior via the configuration object.

    Set configure_lograge_automatically to false to prevent LogBench from overriding your existing lograge settings.

    LogBench.configuration.configure_lograge_automatically = false
  11. Customize LogBench initialization messages

    main

    In development environments, LogBench prints a success message when Rails starts. You can control the verbosity of this message using the show_init_message configuration option.

    Supported values:

    • :full (default): Prints a success message and instructions on how to view logs (e.g., log_bench log/development.log).
    • :min: Prints only the minimal success message: ✅ LogBench is ready to use!
    LogBench.configuration.show_init_message = :min
  12. Required and optional log fields for LogBench

    main

    LogBench requires logs to be in JSON format.

    HTTP Request logs must include:

    • method: HTTP method (GET, POST, etc.)
    • path: Request path
    • status: HTTP status code
    • request_id: Unique request identifier
    • duration: Request duration in milliseconds

    Optional fields for HTTP requests:

    • controller: Controller name
    • action: Action name
    • allocations: Memory allocations
    • view: View rendering time
    • db: Database query time

    Other query logs must include:

    • message: SQL query with timing information
    • request_id: Used to link the query to its parent HTTP request.