lib/dispatch-rider/logging/json_formatter.rb in dispatch-rider-1.6.1 vs lib/dispatch-rider/logging/json_formatter.rb in dispatch-rider-1.6.2

- old
+ new

@@ -1,81 +1,14 @@ require 'json' # JSON Log Formatter module DispatchRider module Logging - class JsonFormatter < BaseFormatter - # @param [DispatchRider::QueueServices::ReceivedMessage] message - # @param [Exception] exception - # @return [String] JSON representation of the log item - def format_error_handler_fail(message, exception) - as_json do - { - phase: :failed, - }.merge exception_info_fragment(message, exception) - end - end + class JsonFormatter - # @param [DispatchRider::QueueServices::ReceivedMessage] message - # @param [String] reason - # @return [String] JSON representation of the log item - def format_got_stop(message, reason) - as_json do - { - phase: :stop, - reason: reason, - }.merge message_info_fragment(message) - end + def format(data) + data.to_json end - # @param [Symbol] kind of log action. one of: :start, :success, :fail, :complete - # @param [DispatchRider::QueueServices::ReceivedMessage] message - # @param [Exception] exception - # @param [Float] Job execution duration - # @return [String] JSON representation of the log item - def format_handling(kind, message, exception: nil, duration: nil) - as_json do - fragment = case kind - when :start - message_info_fragment(message) - when :success - message_info_fragment(message) - when :fail - exception_info_fragment(message, exception) - when :complete - duration_fragment = { duration: format_duration(duration) } - message_info_fragment(message).merge duration_fragment - end - { phase: kind }.merge fragment - end - end - - private - - def format_duration(duration) - duration - end - - def as_json - JSON.generate yield - end - - def message_info_fragment(message) - { - guid: message.guid.to_s, - subject: message.subject, - body: message_info_arguments(message), - } - end - - def exception_info_fragment(message, exception) - exception_details = { - expection: { - class: exception.class.to_s, - message: exception.message, - } - } - message_info_fragment(message).merge exception_details - end end end end