Class: Gcloud::Logging::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/logging/entry.rb,
lib/gcloud/logging/entry/list.rb,
lib/gcloud/logging/entry/operation.rb,
lib/gcloud/logging/entry/http_request.rb

Overview

# Entry

An individual entry in a log.

Each log entry is composed of metadata and a payload. The metadata includes standard information used by Cloud Logging, such as when the entry was created and where it came from. The payload is the event record. Traditionally this is a message string, but in Cloud Logging it can also be a JSON or protocol buffer object. A single log can have entries with different payload types.

A log is a named collection of entries. Logs can be produced by Google Cloud Platform services, by third-party services, or by your applications. For example, the log ‘compute.googleapis.com/activity_log` is produced by Google Compute Engine. Logs are simply referenced by name in Gcloud. There is no `Log` type in Gcloud or `Log` resource in the Cloud Logging API.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging

entry = logging.entry
entry.payload = "Job started."
entry.log_name = "my_app_log"
entry.resource.type = "gae_app"
entry.resource.labels[:module_id] = "1"
entry.resource.labels[:version_id] = "20150925t173233"

logging.write_entries entry

See Also:

Defined Under Namespace

Classes: HttpRequest, List, Operation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEntry

Create a new Entry instance. The #resource attribute is pre-populated with a new Resource instance. See also Project#entry.



65
66
67
68
69
70
71
# File 'lib/gcloud/logging/entry.rb', line 65

def initialize
  @labels = {}
  @resource = Resource.new
  @http_request = HttpRequest.new
  @operation = Operation.new
  @severity = :DEFAULT
end

Instance Attribute Details

#http_requestGcloud::Logging::Entry::HttpRequest (readonly)

Information about the HTTP request associated with this log entry, if applicable.



180
181
182
# File 'lib/gcloud/logging/entry.rb', line 180

def http_request
  @http_request
end

#insert_idObject

A unique ID for the log entry. If you provide this field, the logging service considers other log entries in the same log with the same ID as duplicates which can be removed. If omitted, Cloud Logging will generate a unique ID for this log entry.



162
163
164
# File 'lib/gcloud/logging/entry.rb', line 162

def insert_id
  @insert_id
end

#labelsHash

A set of user-defined data that provides additional information about the log entry.

Returns:

  • (Hash)


168
169
170
# File 'lib/gcloud/logging/entry.rb', line 168

def labels
  @labels
end

#log_nameObject

The resource name of the log to which this log entry belongs. The format of the name is ‘projects/<project-id>/logs/<log-id>`. e.g. `projects/my-projectid/logs/my_app_log` and `projects/1234567890/logs/library.googleapis.com%2Fbook_log`

The log ID part of resource name must be less than 512 characters long and can only include the following characters: upper and lower case alphanumeric characters: ‘[A-Za-z0-9]`; and punctuation characters: forward-slash (`/`), underscore (`_`), hyphen (`-`), and period (`.`). Forward-slash (`/`) characters in the log ID must be URL-encoded.



84
85
86
# File 'lib/gcloud/logging/entry.rb', line 84

def log_name
  @log_name
end

#operationGcloud::Logging::Entry::Operation (readonly)

Information about an operation associated with the log entry, if applicable.



186
187
188
# File 'lib/gcloud/logging/entry.rb', line 186

def operation
  @operation
end

#payloadString, Hash

The log entry payload, represented as either a string, a hash (JSON), or a hash (protocol buffer).

Returns:

  • (String, Hash)


174
175
176
# File 'lib/gcloud/logging/entry.rb', line 174

def payload
  @payload
end

#resourceGcloud::Logging::Resource (readonly)

The monitored resource associated with this log entry. Example: a log entry that reports a database error would be associated with the monitored resource designating the particular database that reported the error.



92
93
94
# File 'lib/gcloud/logging/entry.rb', line 92

def resource
  @resource
end

#severityObject

The severity level of the log entry. The default value is ‘DEFAULT`.



101
102
103
# File 'lib/gcloud/logging/entry.rb', line 101

def severity
  @severity
end

#timestampObject

The time the event described by the log entry occurred. If omitted, Cloud Logging will use the time the log entry is written.



97
98
99
# File 'lib/gcloud/logging/entry.rb', line 97

def timestamp
  @timestamp
end

Class Method Details

.extract_payload(grpc) ⇒ Object



280
281
282
# File 'lib/gcloud/logging/entry.rb', line 280

def self.extract_payload grpc
  grpc.proto_payload || grpc.json_payload || grpc.text_payload
end

.extract_timestamp(grpc) ⇒ Object



286
287
288
289
# File 'lib/gcloud/logging/entry.rb', line 286

def self.extract_timestamp grpc
  return nil if grpc.timestamp.nil?
  Time.at grpc.timestamp.seconds, grpc.timestamp.nanos/1000.0
end

.from_grpc(grpc) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/gcloud/logging/entry.rb', line 222

def self.from_grpc grpc
  return new if grpc.nil?
  new.tap do |e|
    e.log_name = grpc.log_name
    e.timestamp = extract_timestamp(grpc)
    e.severity = grpc.severity
    e.insert_id = grpc.insert_id
    e.labels = GRPCUtils.map_to_hash(grpc.labels)
    e.payload = extract_payload(grpc)
    e.instance_variable_set "@resource", Resource.from_grpc(grpc.resource)
    e.instance_variable_set "@http_request",
                            HttpRequest.from_grpc(grpc.http_request)
    e.instance_variable_set "@operation",
                            Operation.from_grpc(grpc.operation)
  end
end

Instance Method Details

#alert?Boolean

Returns ‘true` if the severity level is `ALERT`.

Returns:

  • (Boolean)


147
148
149
# File 'lib/gcloud/logging/entry.rb', line 147

def alert?
  severity == :ALERT
end

#append_payload(grpc) ⇒ Object

object.



264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/gcloud/logging/entry.rb', line 264

def append_payload grpc
  grpc.proto_payload = nil
  grpc.json_payload  = nil
  grpc.text_payload  = nil

  if payload.is_a? Google::Protobuf::Any
    grpc.proto_payload = payload
  elsif payload.respond_to? :to_hash
    grpc.json_payload = GRPCUtils.hash_to_struct payload.to_hash
  else
    grpc.text_payload = payload.to_s
  end
end

#critical?Boolean

Returns ‘true` if the severity level is `CRITICAL`.

Returns:

  • (Boolean)


141
142
143
# File 'lib/gcloud/logging/entry.rb', line 141

def critical?
  severity == :CRITICAL
end

#debug?Boolean

Returns ‘true` if the severity level is `DEBUG`.

Returns:

  • (Boolean)


111
112
113
# File 'lib/gcloud/logging/entry.rb', line 111

def debug?
  severity == :DEBUG
end

#default?Boolean

Returns ‘true` if the severity level is `DEFAULT`.

Returns:

  • (Boolean)


105
106
107
# File 'lib/gcloud/logging/entry.rb', line 105

def default?
  severity == :DEFAULT
end

#emergency?Boolean

Returns ‘true` if the severity level is `EMERGENCY`.

Returns:

  • (Boolean)


153
154
155
# File 'lib/gcloud/logging/entry.rb', line 153

def emergency?
  severity == :EMERGENCY
end

#empty?Boolean

Returns:

  • (Boolean)


190
191
192
193
194
195
196
197
198
199
# File 'lib/gcloud/logging/entry.rb', line 190

def empty?
  log_name.nil? &&
    timestamp.nil? &&
    insert_id.nil? &&
    (labels.nil? || labels.empty?) &&
    payload.nil? &&
    resource.empty? &&
    http_request.empty? &&
    operation.empty?
end

#error?Boolean

Returns ‘true` if the severity level is `ERROR`.

Returns:

  • (Boolean)


135
136
137
# File 'lib/gcloud/logging/entry.rb', line 135

def error?
  severity == :ERROR
end

#info?Boolean

Returns ‘true` if the severity level is `INFO`.

Returns:

  • (Boolean)


117
118
119
# File 'lib/gcloud/logging/entry.rb', line 117

def info?
  severity == :INFO
end

#labels_grpcObject

Google::Logging::V2::LogEntry object.



253
254
255
256
257
258
259
# File 'lib/gcloud/logging/entry.rb', line 253

def labels_grpc
  # Coerce symbols to strings
  Hash[labels.map do |k, v|
    v = String(v) if v.is_a? Symbol
    [String(k), v]
  end]
end

#notice?Boolean

Returns ‘true` if the severity level is `NOTICE`.

Returns:

  • (Boolean)


123
124
125
# File 'lib/gcloud/logging/entry.rb', line 123

def notice?
  severity == :NOTICE
end

#timestamp_grpcObject



241
242
243
244
245
246
247
248
# File 'lib/gcloud/logging/entry.rb', line 241

def timestamp_grpc
  return nil if timestamp.nil?
  # TODO: ArgumentError if timestamp is not a Time object?
  Google::Protobuf::Timestamp.new(
    seconds: timestamp.to_i,
    nanos: timestamp.nsec
  )
end

#to_grpcObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/gcloud/logging/entry.rb', line 203

def to_grpc
  grpc = Google::Logging::V2::LogEntry.new(
    log_name: log_name.to_s,
    timestamp: timestamp_grpc,
    # TODO: verify severity is the correct type?
    severity: severity,
    insert_id: insert_id.to_s,
    labels: labels_grpc,
    resource: resource.to_grpc,
    http_request: http_request.to_grpc,
    operation: operation.to_grpc
  )
  # Add payload
  append_payload grpc
  grpc
end

#warning?Boolean

Returns ‘true` if the severity level is `WARNING`.

Returns:

  • (Boolean)


129
130
131
# File 'lib/gcloud/logging/entry.rb', line 129

def warning?
  severity == :WARNING
end