Sha256: 389fa7cd6b21d83fbb250d36426068cf29a6978efa774d616a87725a20727625

Contents?: true

Size: 768 Bytes

Versions: 3

Compression:

Stored size: 768 Bytes

Contents

require "active_record"
require "nokogiri"
require "zeitwerk"

loader = Zeitwerk::Loader.for_gem
loader.collapse("#{__dir__}/rails_api_logger")
loader.ignore("#{__dir__}/generators")
loader.setup

class RailsApiLogger
  class Error < StandardError; end

  def initialize(loggable = nil, skip_body: false)
    @loggable = loggable
    @skip_body = skip_body
  end

  def call(url, request)
    log = OutboundRequestLog.from_request(request, loggable: @loggable)
    yield.tap do |response|
      log.from_response(response, skip_body: @skip_body)
    end
  rescue => e
    log.response_body = {error: e.message}
    raise
  ensure
    log.ended_at = Time.current
    log.save!
  end
end

ActiveSupport.on_load(:action_controller) do
  include InboundRequestsLogger
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_api_logger-0.8.2 lib/rails_api_logger.rb
rails_api_logger-0.8.1 lib/rails_api_logger.rb
rails_api_logger-0.8.0 lib/rails_api_logger.rb