Class: Rack::GrayLogger::Middleware

Inherits:
Object
  • Object
show all
Includes:
GrayLogger::Support
Defined in:
lib/gray_logger/rack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GrayLogger::Support

included

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



9
10
11
# File 'lib/gray_logger/rack.rb', line 9

def initialize(app)
  @app = app
end

Instance Attribute Details

#gray_loggerObject

Returns the value of attribute gray_logger.



7
8
9
# File 'lib/gray_logger/rack.rb', line 7

def gray_logger
  @gray_logger
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gray_logger/rack.rb', line 13

def call(env)
  gray_logger = ::GrayLogger.proxy.gray_logger
  if gray_logger.nil?
    @app.call(env)
  else
    begin
      gray_logger.reset!
      status, headers, body = @app.call(env)
    rescue Exception => e

      # Ensure the original exception can be re-raised
      begin
        gray_logger.log_exception(e) if gray_logger.automatic_logging?
      rescue
        $stderr.puts "gray_logger threw an error and shouldn't"
      end

      raise e
    ensure
      begin
        req = Rack::Request.new(env)
        if ::GrayLogger.configuration.automatic_logging?
          gray_logger.log_exception(env['rack.exception']) if env['rack.exception']
          gray_logger.after_request_log.status_code = status.to_i
          gray_logger.after_request_log.short_message = "Request: #{req.path} (#{status.to_i})" if gray_logger.after_request_log[:short_message].nil?
          gray_logger.flush
        else
          gray_logger.reset!
        end
      rescue Exception => e
        $stderr.puts "gray_logger threw an error and shouldn't"
      ensure
        [status, headers, body]
      end
    end
  end
end