mirror of
https://github.com/samsonjs/grape_logging.git
synced 2026-04-04 10:35:47 +00:00
separating out the base logger behaviour and logging of specific parts of request / response into pluggable components also setting the defualt formatter for the logger
31 lines
872 B
Ruby
31 lines
872 B
Ruby
module GrapeLogging
|
|
module Formatters
|
|
class Default
|
|
def call(severity, datetime, _, data)
|
|
"[#{datetime}] #{severity} -- #{format(data)}\n"
|
|
end
|
|
|
|
def format(data)
|
|
if data.is_a?(String)
|
|
data
|
|
elsif data.is_a?(Exception)
|
|
format_exception(data)
|
|
elsif data.is_a?(Hash)
|
|
"#{data.delete(:status)} -- #{format_hash(data.delete(:time))} -- #{data.delete(:method)} #{data.delete(:path)} #{format_hash(data)}"
|
|
else
|
|
data.inspect
|
|
end
|
|
end
|
|
|
|
private
|
|
def format_hash(hash)
|
|
hash.keys.sort.map { |key| "#{key}=#{hash[key]}" }.join(' ')
|
|
end
|
|
|
|
def format_exception(exception)
|
|
backtrace_array = (exception.backtrace || []).map { |line| "\t#{line}" }
|
|
"#{exception.message}\n#{backtrace_array.join("\n")}"
|
|
end
|
|
end
|
|
end
|
|
end
|