Class: Rack::RequestLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/request_logger.rb,
lib/rack/request_logger_version.rb

Constant Summary collapse

VERSION =
'0.0.6'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RequestLogger

Returns a new instance of RequestLogger.



42
43
44
# File 'lib/rack/request_logger.rb', line 42

def initialize(app)
  @app = app
end

Class Method Details

.log_handlerObject



37
38
39
# File 'lib/rack/request_logger.rb', line 37

def log_handler
  @log_handler
end

.log_handler=(log_handler) ⇒ Object



31
32
33
34
35
# File 'lib/rack/request_logger.rb', line 31

def log_handler=(log_handler)
  @log_handler = log_handler
  #Explicity set the logger on this class so everything works when this is inherited from
  Rack::RequestLogger.instance_variable_set(:@log_handler, log_handler)
end

Instance Method Details

#call(env) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rack/request_logger.rb', line 46

def call(env)
  begin
    request_time = Time.now
    request = Rack::Request.new env
    status, response_headers, response_body = @app.call(env)
  rescue => e
    #TODO: Should return a better message, but this should basically never happen anyway,
    #      since @app.call won't throw execeptions, but rather catches and wraps them
    status = 500
    response_headers = {}
    response_body = e.message
    error = e
  end

  if log_handler.respond_to?(:write) && log_request?(env, request, status, response_headers, response_body)
    begin
      params = {
        :url              => request.url,
        :method           => request.request_method,
        :request_headers  => format_header_hash(filter_request_headers(request_headers(env))),
        :request_body     => filter_request_body(read_stream(request.body)),
        :request_time     => request_time,
        :status           => status,
        :response_headers => format_header_hash(filter_response_headers(response_headers)),
        :response_body    => filter_response_body(response_body_to_s(response_body)),
        :response_time    => Time.now
      }

      remote_host = remote_host(env)
      params[:remote_host] = remote_host if remote_host

      error ||= app_error(env)
      params[:error] = format_error(error) if error

      add_log_params(env, status, response_headers, response_body, params)

      log_handler.write params
    rescue => e
      begin
        log_handler.write(
          #TODO: Log more things that are safe, or ensure they are safe first
          :error => format_error(e)
        )
      rescue
      end
    end
  end

  [status, response_headers, response_body]
end