Sha256: da027a4531207b3670f03f544d1eafe5964b94722efeea0c76c6660bbc4096f9

Contents?: true

Size: 1.08 KB

Versions: 13

Compression:

Stored size: 1.08 KB

Contents

module Locomotive
  module API
    module Middlewares

      class LoggerMiddleware

        def initialize(app)
          @app = app
        end

        def call(env)
          @app.call(env).tap do |response|
            log_message(response, env)
          end
        end

        private

        def log_message(response, env)
          return unless Rails.env.development? || Rails.env.test?

          message = payload(response, env).map { |p| "#{p.first}=\"#{p.last}\"" }.join(' ')

          Rails.logger.debug(message)
        end

        def payload(response, env)
          params = env['api.endpoint'].params.to_hash
          params.delete_if { |k, _| %w(route_info format).include?(k) }

          [
            [:service,    'api.request'],
            [:method,     env['REQUEST_METHOD']],
            [:endpoint,   env['PATH_INFO']],
            [:params,     params.inspect],
            [:headers,    env.select { |k, v| k.start_with? 'HTTP_X' }],
            [:status,     response[0]],
            [:timestamp,  Time.zone.now]
          ]
        end

      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
locomotivecms-3.0.1 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.0.0 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.0.0.rc7 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.0.0.rc6 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.0.0.rc5 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.0.0.rc4 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.0.0.rc3 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.0.0.rc2 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.0.0.rc1 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.0.0.pre.beta.1 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.0.0.pre.alpha.3 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.0.0.pre.alpha.2 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.0.0.pre.alpha app/api/locomotive/api/middlewares/logger_middleware.rb