Class: Rack::Httperflog

Inherits:
Struct
  • Object
show all
Defined in:
lib/rack/httperflog.rb

Overview

Httperflog is a middleware that will log the request and all the img, iframes, css and js paths founds in its body if the response is an html document in a format appropriate for using it as an httperf session log.

use Httperflog :ping_urls, “log/path”, “flag/path”

ping_urls :: pass :ping_urls for making the middleware perform an http get
             to validate each path found inside the html body. WARNING: only use if your
             http server can handle more than one concurrent connection (e.g.: you are
             using passenger or have a cluster of load balanced mongrels). Any other value
             avoids performing the requests.
log_path  :: path to the log file that will contain the session log.
flag_path :: the logging will be performed only when the file pointed by
             flag_path exists.

Defined Under Namespace

Modules: UrlHelper Classes: HtmlPathsExtractor, Logger

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject

Returns the value of attribute app

Returns:

  • (Object)

    the current value of app



20
21
22
# File 'lib/rack/httperflog.rb', line 20

def app
  @app
end

#flag_pathObject

Returns the value of attribute flag_path

Returns:

  • (Object)

    the current value of flag_path



20
21
22
# File 'lib/rack/httperflog.rb', line 20

def flag_path
  @flag_path
end

#log_pathObject

Returns the value of attribute log_path

Returns:

  • (Object)

    the current value of log_path



20
21
22
# File 'lib/rack/httperflog.rb', line 20

def log_path
  @log_path
end

#ping_urlsObject

Returns the value of attribute ping_urls

Returns:

  • (Object)

    the current value of ping_urls



20
21
22
# File 'lib/rack/httperflog.rb', line 20

def ping_urls
  @ping_urls
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/rack/httperflog.rb', line 21

def call(env)
  status, headers, response = app.call(env)

  Httperflog::Logger.new(request_to_log(env), response_to_log(status, headers, response), log_file, ping_urls).perform_logging if logging_enabled?

  [status, headers, response]
end

#log_fileObject

Log file opened on append mode.



35
36
37
# File 'lib/rack/httperflog.rb', line 35

def log_file
  @lof_file ||= ::File.open(log_path, "a")
end

#logging_enabled?Boolean

Logging will be enabled if file in flag_path exists.

Returns:

  • (Boolean)


30
31
32
# File 'lib/rack/httperflog.rb', line 30

def logging_enabled?
  ::File.file?(flag_path)
end

#request_to_log(env) ⇒ Object

Extract the Rack::Request object needed by the logger



40
41
42
# File 'lib/rack/httperflog.rb', line 40

def request_to_log(env)
  env["rack.request"].class <= Rack::Request ? env["rack.request"] : Rack::Request.new(env)
end

#response_to_log(status, headers, response) ⇒ Object

Extract the Rack::Response object needed by the logger



45
46
47
48
49
# File 'lib/rack/httperflog.rb', line 45

def response_to_log(status, headers, response)
  r = response.class <= Rack::Response ? response : Rack::Response.new(response, status, headers)
  r.status = r.status.to_i # avoid problems with Rack::Response methods that expect the status code to be an integer.
  r
end