Sha256: 3fb2406c6dd6b791048dacd4a939d540256482aad82c902645669cb1e5a272b3

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

module LogSanity
  class RequestLogger

    def initialize(app)
      @app = app
    end

    def call(env)
      request = ActionDispatch::Request.new(env)

      conditionally_silence(request) do |silence|
        payload = {env: env, request: request, silence: silence}
        handle = start(payload)
        begin
          status, headers, body = response = @app.call(env)
          payload[:response] = response
          body = Rack::BodyProxy.new(body){ handle.finish }
          if response.frozen?
            [status, headers, body]
          else
            response[2] = body
            response
          end
        rescue Exception => e
          payload[:exception] = e
          handle.finish
          raise e
        end
      end
    ensure
      ActiveSupport::LogSubscriber.flush_all!
    end

    def conditionally_silence(request)
      if silence = silence_path?(request)
        logger.silence do
          yield silence
        end
      else
        yield silence
      end
    end



    private

    def start(payload)
      LogSanity.reset_fields
      instrumenter = ActiveSupport::Notifications.instrumenter
      instrumenter.build_handle('request.action_dispatch', payload).tap do |handle|
        handle.start
      end
    end

    def silence_path?(request)
      Rails.application.config.logsanity.silence_paths.any? do |s|
        case s
        when Regexp
          s =~ request.path
        when String
          s == request.path
        end
      end
    end

    def logger
      Rails.logger
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
log_sanity-2.1.0 lib/log_sanity/middleware/request_logger.rb
log_sanity-2.0.0 lib/log_sanity/middleware/request_logger.rb