class Loggie::Extract
Attributes
keep_fields[R]
Public Class Methods
new()
click to toggle source
Extracts the payload from the response into a more manageable hash.
-
Converts the JSON into a Hash
-
Strips any empty
-
Updates the timestamp from unix to DateTime
-
Removes fields that are not interesting
The full log can contain many fields, most are not that great, so are excluded by default
Full response hash includes :
"remote_addr"=>"11.11.11.161", "request_method"=>"GET", "path_info"=>"/foo/user", "query_string"=>"device_id=00000000-0000-0000-0000-000000000000", "version"=>"HTTP/1.1", "host"=>"server.com", "origin"=>nil, "connection"=>nil, "proxy_connection"=>nil, "referer"=>nil, "x_forwared_for"=>"11.111.232.161", "x_forwared_proto"=>"https", "x_forwared_port"=>"443", "accept"=>"*/*", "accept_encoding"=>"gzip, deflate", "accept_language"=>"en-gb", "content_length"=>nil, "content_type"=>nil, "agent"=>"version", "user_agent"=>"app", "authorization"=>"bearer token", "api_version"=>"20161115", "response_status"=>200, "response_header"=>{"Content-Type"=>"application/json", "Content-Length"=>"819"}, "response_body" .... "request_params" .... "duration"
# File lib/loggie/extract.rb, line 39 def initialize @keep_fields = Loggie.configuration.default_fields_included end
Public Instance Methods
call(results)
click to toggle source
@param [Array] results hash from the request
# File lib/loggie/extract.rb, line 45 def call(results) return if results.nil? results.map do |result| formatted_message = format(result["message"]) next if formatted_message.empty? { timestamp: formatted_date(result["timestamp"]), message: formatted_message } end.compact end
Private Instance Methods
format(m)
click to toggle source
# File lib/loggie/extract.rb, line 67 def format(m) m = remove_rails_timestamp(m) m = safe_parse(m) m = remove_empty_fields(m) m.slice(*keep_fields) end
formatted_date(timestamp)
click to toggle source
# File lib/loggie/extract.rb, line 63 def formatted_date(timestamp) Time.at(timestamp / 1000).to_datetime end
remove_empty_fields(m)
click to toggle source
# File lib/loggie/extract.rb, line 86 def remove_empty_fields(m) m.compact.reject { |m| m.empty? } end
remove_rails_timestamp(message)
click to toggle source
# File lib/loggie/extract.rb, line 74 def remove_rails_timestamp(message) message.sub(/.*-- : /, '') end
safe_parse(message, save: true)
click to toggle source
# File lib/loggie/extract.rb, line 78 def safe_parse(message, save: true) begin JSON.parse message rescue JSON::ParserError {} end end