Class: Rack::Httperflog::Logger

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

Overview

Extracts the lines to log from the rack request and response and logs them to a file on log_path path.

Logger.new(“log/path”, :ping_urls, request, response)

log_path

path to the log file to log the lines

ping_urls

provide :ping_urls to perform an http get to each path found inside the body of the response. Provide any other value to avoid this check.

request

a Rack::Request instance

response

a Rack::Response instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fileObject

Returns the value of attribute file

Returns:

  • (Object)

    the current value of file



61
62
63
# File 'lib/rack/httperflog.rb', line 61

def file
  @file
end

#ping_urlsObject

Returns the value of attribute ping_urls

Returns:

  • (Object)

    the current value of ping_urls



61
62
63
# File 'lib/rack/httperflog.rb', line 61

def ping_urls
  @ping_urls
end

#requestObject

Returns the value of attribute request

Returns:

  • (Object)

    the current value of request



61
62
63
# File 'lib/rack/httperflog.rb', line 61

def request
  @request
end

#responseObject

Returns the value of attribute response

Returns:

  • (Object)

    the current value of response



61
62
63
# File 'lib/rack/httperflog.rb', line 61

def response
  @response
end

Instance Method Details

#each {|"#{ request.path_info } method=#{ request.request_method.to_s.upcase }#{ form_data }\n"| ... } ⇒ Object

yields each line for the log

Yields:

  • ("#{ request.path_info } method=#{ request.request_method.to_s.upcase }#{ form_data }\n")


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rack/httperflog.rb', line 70

def each
  return if response.server_error? || response.client_error?

  form_data = " contents=\"#{ Rack::Utils.build_query(request.params) }\"" if request.form_data? && !request.params.empty?

  yield "#{ request.path_info } method=#{ request.request_method.to_s.upcase }#{ form_data }\n"

  paths_to_log.each do |path|
    yield "    #{ path }\n" if path != request.path_info && (ping_urls != :ping_urls || UrlHelper.url_status_is?("200", request.host, request.port, path))
  end
end

#paths_to_logObject

uses a HtmlPathsExtractor object to parse the response if it contains html, and extracts all the relevant paths.



83
84
85
# File 'lib/rack/httperflog.rb', line 83

def paths_to_log
  response.content_type =~ /html/ ? HtmlPathsExtractor.new(response.body).paths : Array.new
end

#perform_loggingObject

Perform the logging of each log line to a file



64
65
66
67
# File 'lib/rack/httperflog.rb', line 64

def perform_logging
  each { |line| file << line }
  file.flush
end