Sha256: d7383a30509ff53d5355a45323cb2b95946285c170e5a4bd6a85d321275d56d7

Contents?: true

Size: 1.01 KB

Versions: 14

Compression:

Stored size: 1.01 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)
          message = payload(response, env).map { |p| "#{p.first}=\"#{p.last}\"" }.join(' ')

          Rails.logger.info(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

14 entries across 14 versions & 1 rubygems

Version Path
locomotivecms-3.3.0 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.3.0.rc3 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.3.0.rc2 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.1.2 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.2.1 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.3.0.rc1 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.2.0 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.2.0.rc2 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.2.0.rc1 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.1.1 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.1.0 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.1.0.rc3 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.1.0.rc2 app/api/locomotive/api/middlewares/logger_middleware.rb
locomotivecms-3.1.0.rc1 app/api/locomotive/api/middlewares/logger_middleware.rb