Class: SmartProxyDynflowCore::LoggerMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_proxy_dynflow_core/logger_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ LoggerMiddleware

Returns a new instance of LoggerMiddleware.



3
4
5
6
# File 'lib/smart_proxy_dynflow_core/logger_middleware.rb', line 3

def initialize(app)
  @logger = SmartProxyDynflowCore::Log.instance
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/smart_proxy_dynflow_core/logger_middleware.rb', line 8

def call(env)
  before = Time.now.to_f
  status = 500
  env['rack.logger'] = @logger
  @logger.info { "Started #{env['REQUEST_METHOD']} #{env['PATH_INFO']} #{env['QUERY_STRING']}" }
  @logger.debug { 'Headers: ' + env.select { |k, v| k.start_with? 'HTTP_' }.inspect }
  if @logger.debug? && env['rack.input']
    body = env['rack.input'].read
    @logger.debug('Body: ' + body) unless body.empty?
    env['rack.input'].rewind
  end
  status, = @app.call(env)
rescue Exception => e
  Log.exception "Error processing request '#{::Logging.mdc['request']}", e
  raise e
ensure
  @logger.info do
    after = Time.now.to_f
    duration = (after - before) * 1000
    "Finished #{env['REQUEST_METHOD']} #{env['PATH_INFO']} with #{status} (#{duration.round(2)} ms)"
  end
end