diff --git a/lib/grape_logging/formatters/json.rb b/lib/grape_logging/formatters/json.rb new file mode 100644 index 0000000..1ea150e --- /dev/null +++ b/lib/grape_logging/formatters/json.rb @@ -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