Sha256: eb2f19151b86ca0be6b314f4e0c4e02330f63dbe1e91807e8f5c349f57eaf602

Contents?: true

Size: 1.47 KB

Versions: 8

Compression:

Stored size: 1.47 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|
        begin
          start(request: request)
          resp = @app.call(env)
          resp[2] = Rack::BodyProxy.new(resp[2]) do
            finish(env: env, request: request, response: resp, silence: silence)
          end
          resp
        rescue Exception => e
          finish(env: env, request: request, exception: e, silence: silence)
          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(params)
      LogSanity.reset_fields
      instrumenter = ActiveSupport::Notifications.instrumenter
      instrumenter.start 'request.action_dispatch', params
    end

    def finish(params)
      instrumenter = ActiveSupport::Notifications.instrumenter
      instrumenter.finish 'request.action_dispatch', params
    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

8 entries across 8 versions & 1 rubygems

Version Path
log_sanity-1.3.2 lib/log_sanity/middleware/request_logger.rb
log_sanity-1.3.1 lib/log_sanity/middleware/request_logger.rb
log_sanity-1.3.0 lib/log_sanity/middleware/request_logger.rb
log_sanity-1.2.0 lib/log_sanity/middleware/request_logger.rb
log_sanity-1.1.1 lib/log_sanity/middleware/request_logger.rb
log_sanity-1.1.0 lib/log_sanity/middleware/request_logger.rb
log_sanity-1.0.0 lib/log_sanity/middleware/request_logger.rb
log_sanity-0.2.5 lib/log_sanity/middleware/request_logger.rb