lib/timber/events/custom.rb in timber-2.5.1 vs lib/timber/events/custom.rb in timber-2.6.0.pre.beta1
- old
+ new
@@ -21,24 +21,27 @@
# snake case. Example: `:my_custom_event`.
# @option attributes [String] :message *required* The message to be logged.
# @option attributes [Hash] :data A hash of JSON encodable data to be stored with the
# log line.
def initialize(attributes)
- @type = attributes[:type] || raise(ArgumentError.new(":type is required"))
- @message = attributes[:message] || raise(ArgumentError.new(":message is required"))
+ normalizer = Util::AttributeNormalizer.new(attributes)
+ @type = normalizer.fetch!(:type, :symbol)
+ @message = normalizer.fetch!(:message, :string)
- data = attributes[:data]
+ data = normalizer.fetch!(:data, :hash)
- if data.is_a?(Hash) && data[:time_ms].is_a?(Time)
+ if !data.nil? && data[:time_ms].is_a?(Time)
data[:time_ms] = Timer.duration_ms(data[:time_ms])
@message << " in #{data[:time_ms]}ms"
end
@data = data
end
def to_hash
- {Timber::Util::Object.try(type, :to_sym) => data}
+ @to_hash ||= Util::NonNilHashBuilder.build do |h|
+ h.add(type, data)
+ end
end
alias to_h to_hash
# Builds a hash representation containing simple objects, suitable for serialization (JSON).
def as_json(_options = {})
\ No newline at end of file