lib/timber/log_entry.rb in timber-2.5.1 vs lib/timber/log_entry.rb in timber-2.6.0.pre.beta1

- old
+ new

@@ -11,11 +11,11 @@ BINARY_LIMIT_THRESHOLD = 1_000.freeze DT_PRECISION = 6.freeze MESSAGE_MAX_BYTES = 8192.freeze SCHEMA = "https://raw.githubusercontent.com/timberio/log-event-json-schema/v3.2.0/schema.json".freeze - attr_reader :context_snapshot, :event, :level, :message, :progname, :tags, :time, :time_ms + attr_reader :context_snapshot, :event, :level, :message, :progname, :tags, :time # Creates a log entry suitable to be sent to the Timber API. # @param level [Integer] the log level / severity # @param time [Time] the exact time the log message was written # @param progname [String] the progname scope for the log message @@ -34,70 +34,46 @@ # This follows the default behavior set by ::Logger # See: https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L615 @message = message.is_a?(String) ? message : message.inspect @message = @message.byteslice(0, MESSAGE_MAX_BYTES) @tags = options[:tags] - @time_ms = options[:time_ms] @context_snapshot = context_snapshot @event = event end # Builds a hash representation containing simple objects, suitable for serialization (JSON). def as_json(options = {}) options ||= {} hash = { :level => level, :dt => formatted_dt, - :message => message, - :tags => tags, - :time_ms => time_ms + :message => message } + if !tags.nil? && tags.length > 0 + hash[:tags] = tags + end + if !event.nil? hash[:event] = event.as_json end if !context_snapshot.nil? && context_snapshot.length > 0 hash[:context] = context_snapshot end hash[:"$schema"] = SCHEMA - hash = if options[:only] + if options[:only] hash.select do |key, _value| options[:only].include?(key) end elsif options[:except] hash.select do |key, _value| !options[:except].include?(key) end else hash - end - - # Preparing a log event for JSON should remove any blank values. Timber strictly - # validates incoming data, including message size. Blank values will fail validation. - # Moreover, binary data (ASCII-8BIT) generally cannot be encoded into JSON because it - # contains characters outside of the valid UTF-8 space. - Util::Hash.deep_reduce(hash) do |k, v, h| - # Discard blank values - if !v.nil? && (!v.respond_to?(:length) || v.length > 0) - # If the value is a binary string, give it special treatment - if v.respond_to?(:encoding) && v.encoding == ::Encoding::ASCII_8BIT - # Only keep binary values less than a certain size. Sizes larger than this - # are almost always file uploads and data we do not want to log. - if v.length < BINARY_LIMIT_THRESHOLD - # Attempt to safely encode the data to UTF-8 - encoded_value = encode_string(v) - if !encoded_value.nil? - h[k] = encoded_value - end - end - else - # Keep all other values - h[k] = v - end - end end end def inspect to_s \ No newline at end of file