Class: Google::Logging::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/google/logging/message.rb

Overview

A log message that can be formatted either as a normal text log entry or as a structured log entry suitable for Google Cloud Logging.

A log message has a “body” which consists of either a string message, a JSON object (i.e. a Hash whose values are typically strings or numbers but could be nested arrays and hashes), or both. It also includes several additional optional fields used by the Google Cloud Logging backend.

Most log formatters will render the message body as a string and ignore the other attributes. The StructuredFormatter, however, will format the full message data in the JSON format understood by the Google logging agent.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message: nil, fields: nil, timestamp: nil, source_location: nil, insert_id: nil, trace: nil, span_id: nil, trace_sampled: nil, labels: nil) ⇒ Message

Low-level constructor for a logging message. All arguments are optional, with the exception that at least one of :message and :fields must be provided.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/google/logging/message.rb', line 98

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

Instance Attribute Details

#fieldsHash{String=>Object}? (readonly)



137
138
139
# File 'lib/google/logging/message.rb', line 137

def fields
  @fields
end

#full_messageString (readonly) Also known as: inspect



130
131
132
# File 'lib/google/logging/message.rb', line 130

def full_message
  @full_message
end

#insert_idString? (readonly)



155
156
157
# File 'lib/google/logging/message.rb', line 155

def insert_id
  @insert_id
end

#labelsHash{String=>String}? (readonly)



179
180
181
# File 'lib/google/logging/message.rb', line 179

def labels
  @labels
end

#messageString (readonly) Also known as: to_s



123
124
125
# File 'lib/google/logging/message.rb', line 123

def message
  @message
end

#source_locationSourceLocation? (readonly)



149
150
151
# File 'lib/google/logging/message.rb', line 149

def source_location
  @source_location
end

#span_idString? (readonly)



167
168
169
# File 'lib/google/logging/message.rb', line 167

def span_id
  @span_id
end

#timestampTime? (readonly)



143
144
145
# File 'lib/google/logging/message.rb', line 143

def timestamp
  @timestamp
end

#traceString? (readonly)



161
162
163
# File 'lib/google/logging/message.rb', line 161

def trace
  @trace
end

#trace_sampledtrue, ... (readonly) Also known as: trace_sampled?



173
174
175
# File 'lib/google/logging/message.rb', line 173

def trace_sampled
  @trace_sampled
end

Class Method Details

.from(input) ⇒ Message

Create a log message from an input object, which can be any of:

  • An existing Message object.

  • A Hash. Symbol keys are used as keyword arguments to the #initialize constructor. String keys are treated as fields in the JSON payload.

  • Any other object is converted to a string with to_s and used as a simple text payload.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/google/logging/message.rb', line 50

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

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/google/logging/message.rb', line 200

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

#hashObject



215
216
217
# File 'lib/google/logging/message.rb', line 215

def hash
  [message, fields, timestamp, source_location, insert_id, trace, span_id, trace_sampled, labels].hash
end

#to_hHash



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/google/logging/message.rb', line 185

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