Class: Rack::JsonLogFormatter

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

Overview

Formats request log from rack env to json

Instance Method Summary collapse

Constructor Details

#initialize(env, headers, status, began_at) ⇒ JsonLogFormatter

Returns a new instance of JsonLogFormatter.



15
16
17
18
19
20
# File 'lib/rack/json_common_logger.rb', line 15

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

Instance Method Details

#messageObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rack/json_common_logger.rb', line 26

def message
  now = Time.now.utc
  {
    timestamp: now.strftime('%Y-%m-%dT%H:%M:%SZ'),
    method: env[REQUEST_METHOD],
    remote_address: remote_address,
    location: location,
    params: (env['rack.request.form_hash'] || {}),
    query_string: query_string,
    status: status.to_s[0..3],
    length: extract_content_length,
    duration: (now - began_at)
  }
end

#to_jsonObject



22
23
24
# File 'lib/rack/json_common_logger.rb', line 22

def to_json
  "#{message.to_json}\n"
end