You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails日志配置:将api/v1接口请求日志输出至api.log

How to Route api/v1 Request Logs to log/api.log in Rails

Got it, let's get your api/v1 requests logging to a separate file instead of the default development.log. Here are two reliable, Rails-friendly ways to make this happen:

Method 1: Controller-Based Approach (Clean if you have an API base controller)

If all your v1 API controllers inherit from a shared base controller (like Api::V1::BaseController), this is the most straightforward method:

  1. Set up your dedicated API logger in an initializer. Create config/initializers/api_logger.rb:

    # Initialize a daily-rotated logger for API requests, matching default Rails log settings
    ApiLogger = ActiveSupport::Logger.new(Rails.root.join('log', 'api.log'), 'daily')
    ApiLogger.formatter = Rails.logger.formatter
    ApiLogger.level = Rails.logger.level
    

    This ensures the API log uses the same format and verbosity as your default logs, with daily rotation to keep file sizes manageable.

  2. Override the logger in your API base controller (app/controllers/api/v1/base_controller.rb):

    class Api::V1::BaseController < ApplicationController
      # Force all child controllers under Api::V1:: to use the custom API logger
      def logger
        ApiLogger
      end
    end
    

    Any controller that inherits from this base (like your TestsController) will now log all requests to log/api.log.

Method 2: Middleware-Based Approach (Works for any path-based routing)

If you don't have a shared API base controller, or want to target requests purely by their URL path, use a custom middleware:

  1. Create the middleware file at app/middleware/api_log_middleware.rb:

    class ApiLogMiddleware
      def initialize(app)
        @app = app
        @default_logger = Rails.logger
        # Initialize API logger with same settings as default
        @api_logger = ActiveSupport::Logger.new(Rails.root.join('log', 'api.log'), 'daily')
        @api_logger.formatter = @default_logger.formatter
        @api_logger.level = @default_logger.level
      end
    
      def call(env)
        # Swap logger if request starts with /api/v1
        if env['PATH_INFO'].start_with?('/api/v1')
          Rails.logger = @api_logger
        end
    
        # Process the request as normal
        response = @app.call(env)
    
        # Restore default logger after request completes
        Rails.logger = @default_logger
    
        response
      end
    end
    
  2. Register the middleware in config/application.rb:

    module YourAppName
      class Application < Rails::Application
        # ... other existing configs ...
        # Insert before Rails' built-in logger middleware to ensure swap takes effect
        config.middleware.insert_before Rails::Rack::Logger, ApiLogMiddleware
      end
    end
    

Test It Out

After setting up either method:

  • Restart your Rails server
  • Send a request to your v1/tests/test endpoint
  • Check log/api.log—you’ll see the full request details there, and they won’t appear in development.log anymore.

Quick Notes

  • For production, you can adjust log rotation rules (like retention days) in the ActiveSupport::Logger initialization.
  • If you use structured logging tools (like Lograge), update the ApiLogger.formatter to match your preferred structure.

内容的提问来源于stack exchange,提问作者AnCo ngCo Danh Bun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:19:15