Class: Depthcharge::RequestLogger

Inherits:
Object
  • Object
show all
Includes:
Filters, Formatters
Defined in:
lib/depthcharge/request_logger.rb

Constant Summary

Constants included from Formatters

Formatters::MAIN_INDENT, Formatters::NEWLINE, Formatters::SUB_INDENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Filters

#request_headers, #request_params

Methods included from Formatters

#blank_line, #format_body, #format_hash, #format_line, #format_lines, #indent

Constructor Details

#initialize(env, status, headers, body) ⇒ RequestLogger

Returns a new instance of RequestLogger.



9
10
11
12
13
14
# File 'lib/depthcharge/request_logger.rb', line 9

def initialize(env, status, headers, body)
  @env = env
  @status = status
  @headers = headers
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/depthcharge/request_logger.rb', line 7

def body
  @body
end

#envObject (readonly)

Returns the value of attribute env.



7
8
9
# File 'lib/depthcharge/request_logger.rb', line 7

def env
  @env
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/depthcharge/request_logger.rb', line 7

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/depthcharge/request_logger.rb', line 7

def status
  @status
end

Instance Method Details

#construct_log_entryObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/depthcharge/request_logger.rb', line 25

def construct_log_entry
  output = ""
  output << blank_line(0)
  output << format_line("#{env["REQUEST_METHOD"]} #{status} #{env["PATH_INFO"].inspect} at #{Time.now}", 0)
  output << format_hash("PARAMS", request_params(env))
  output << blank_line
  output << format_hash("REQUEST HEADERS", request_headers(env))
  output << blank_line
  output << format_line("RESPONSE STATUS: #{status}")
  output << blank_line
  output << format_hash("RESPONSE HEADERS", headers)
  output << blank_line
  output << format_line("BODY:")
  output << format_body(headers, body, 2)
  output << blank_line
  output << blank_line(0)
end

#log(*outputs) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/depthcharge/request_logger.rb', line 16

def log(*outputs)
  log_entry = construct_log_entry

  outputs.flatten.each do |output|
    output.puts(log_entry)
    output.flush
  end
end