class Google::Logging::Message


agent.
full message data in the JSON format understood by the Google logging
the other attributes. The {StructuredFormatter}, however, will format the
Most log formatters will render the message body as a string and ignore
additional optional fields used by the Google Cloud Logging backend.
but could be nested arrays and hashes), or both. It also includes several
JSON object (i.e. a Hash whose values are typically strings or numbers
A log message has a “body” which consists of either a string message, a
as a structured log entry suitable for Google Cloud Logging.
A log message that can be formatted either as a normal text log entry or
#

def self.from input

Returns:
  • (Message) -

Parameters:
  • input (Object) -- A log message input object.
def self.from input
  case input
  when Hash
    kwargs = {}
    fields = nil
    input.each do |k, v|
      if k.is_a? Symbol
        kwargs[k] = v
      else
        (fields ||= {})[k.to_s] = v
      end
    end
    if kwargs[:fields] && fields
      kwargs[:fields].merge! fields
    else
      kwargs[:fields] ||= fields
    end
    new(**kwargs)
  when Message
    input
  else
    new message: input.to_s
  end
end

def == other

Other tags:
    Private: -
def == other
  return false unless other.is_a? Message
  message == other.message &&
    fields == other.fields &&
    timestamp == other.timestamp &&
    source_location == other.source_location &&
    insert_id == other.insert_id &&
    trace == other.trace &&
    span_id == other.span_id &&
    trace_sampled? == other.trace_sampled? &&
    labels == other.labels
end

def hash

Other tags:
    Private: -
def hash
  [message, fields, timestamp, source_location, insert_id, trace, span_id, trace_sampled, labels].hash
end

def initialize message: nil,

Parameters:
  • labels (Hash{String=>String}) -- Optional metadata.
  • trace_sampled (boolean) -- Whether this trace is sampled. Optional.
  • span_id (String) -- The trace span containing this entry. Optional.
  • trace (String) -- A Google Cloud Trace resource name. Optional.
  • insert_id (String) -- A unique ID for this log entry that could be
  • source_location (SourceLocation, Hash, :caller, nil) -- The source
  • timestamp (Time, Numeric, :now) -- The timestamp for the log entry.
  • fields (Hash{String=>Object}) -- The log message as a set of
  • message (String) -- The main log message as a string.
def initialize message: nil,
               fields: nil,
               timestamp: nil,
               source_location: nil,
               insert_id: nil,
               trace: nil,
               span_id: nil,
               trace_sampled: nil,
               labels: nil
  @fields = interpret_fields fields
  @message, @full_message = interpret_message message, @fields
  @timestamp = interpret_timestamp timestamp
  @source_location = interpret_source_location source_location
  @insert_id = interpret_string insert_id
  @trace = interpret_string trace
  @span_id = interpret_string span_id
  @trace_sampled = interpret_boolean trace_sampled
  @labels = interpret_labels labels
end

def interpret_boolean input

def interpret_boolean input
  return nil if input.nil?
  input ? true : false
end

def interpret_fields fields

def interpret_fields fields
  return nil if fields.nil?
  fields = normalize_json fields
  fields.each_key do |key|
    if DISALLOWED_FIELDS.include?(key) || key.start_with?("logging.googleapis.com/")
      raise ArgumentError, "Field key not allowed: #{key}"
    end
  end
  fields
end

def interpret_labels input

def interpret_labels input
  return nil if input.nil?
  input.to_h do |k, v|
    v_converted =
      case v
      when Array, Hash
        JSON.generate v
      else
        v.to_s
      end
    [k.to_s, v_converted]
  end
end

def interpret_message message, fields

def interpret_message message, fields
  if message
    message = message.to_s
    full_message = fields ? "#{message} -- #{JSON.generate fields}" : message
    [message, full_message]
  else
    fields_json = JSON.generate fields
    [fields_json, fields_json]
  end
end

def interpret_source_location source_location

def interpret_source_location source_location
  case source_location
  when SourceLocation
    source_location
  when Hash
    SourceLocation.new(**source_location)
  when :caller
    SourceLocation.for_caller omit_files: [__FILE__]
  end
end

def interpret_string input

def interpret_string input
  input&.to_s
end

def interpret_timestamp timestamp

def interpret_timestamp timestamp
  case timestamp
  when Time
    timestamp
  when Numeric
    Time.at timestamp
  when :now
    Time.now.utc
  end
end

def normalize_json input

def normalize_json input
  case input
  when Hash
    input.to_h { |k, v| [k.to_s, normalize_json(v)] }
  when Array
    input.map { |v| normalize_json v }
  when Integer, Float, nil, true, false
    input
  when Rational
    if input.denominator == 1
      input.numerator
    else
      input.to_f
    end
  else
    input.to_s
  end
end

def to_h

Returns:
  • (Hash) - A hash of kwargs that can be passed to the constructor
def to_h
  {
    message: @message,
    fields: @fields.dup,
    timestamp: @timestamp,
    source_location: @source_location,
    insert_id: @insert_id,
    trace: @trace,
    span_id: @span_id,
    trace_sampled: @trace_sampled,
    labels: @labels.dup
  }
end