mirror of
https://github.com/samsonjs/grape_logging.git
synced 2026-03-30 09:45:46 +00:00
Add JSON formatter
This commit is contained in:
parent
de4d5c2c0f
commit
2e0f8c2aa5
1 changed files with 33 additions and 0 deletions
33
lib/grape_logging/formatters/json.rb
Normal file
33
lib/grape_logging/formatters/json.rb
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
module GrapeLogging
|
||||
module Formatters
|
||||
class Json
|
||||
def call(severity, datetime, _, data)
|
||||
JSON.parse({
|
||||
date: datetime,
|
||||
severity: severity,
|
||||
data: format(data)
|
||||
})
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def format(data)
|
||||
if data.is_a?(String) || data.is_a?(Hash)
|
||||
data
|
||||
elsif data.is_a?(Exception)
|
||||
format_exception(data)
|
||||
else
|
||||
data.inspect
|
||||
end
|
||||
end
|
||||
|
||||
def format_exception(exception)
|
||||
{
|
||||
exception: {
|
||||
message: exception.message
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue