lib/timber/events/custom.rb in timber-2.0.24 vs lib/timber/events/custom.rb in timber-2.1.0.rc1
- old
+ new
@@ -1,5 +1,8 @@
+require "timber/event"
+require "timber/util"
+
module Timber
module Events
# Allows for custom events that aren't covered elsewhere.
#
# Custom events can be used to encode information about events that are central
@@ -20,23 +23,28 @@
# @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"))
- @data = attributes[:data]
+
+ data = attributes[:data]
+
+ if data.is_a?(Hash) && 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}
end
alias to_h to_hash
+ # Builds a hash representation of containing simply objects, suitable for serialization.
def as_json(_options = {})
{:custom => to_hash}
- end
-
- def to_json(options = {})
- as_json().to_json(options)
end
end
end
end
\ No newline at end of file