Class: Gcloud::Logging::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/logging/project.rb

Overview

Project

Google Cloud Logging collects and stores logs from applications and services on the Google Cloud Platform.

See Gcloud#logging

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
# ...

Instance Method Summary collapse

Instance Method Details

#create_metric(name, description: nil, filter: nil) ⇒ Gcloud::Logging::Metric Also known as: new_metric

Creates a new metric.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
metric = logging.create_metric "my-metric"

519
520
521
522
523
524
525
526
527
# File 'lib/gcloud/logging/project.rb', line 519

def create_metric name, description: nil, filter: nil
  ensure_connection!
  resp = connection.create_metric name, description, filter
  if resp.success?
    Metric.from_gapi resp.data, connection
  else
    fail ApiError.from_response(resp)
  end
end

#create_sink(name, destination: nil, filter: nil, version: :unspecified) ⇒ Gcloud::Logging::Sink Also known as: new_sink

Creates a new sink.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
sink = logging.create_sink "my-sink"

407
408
409
410
411
412
413
414
415
416
# File 'lib/gcloud/logging/project.rb', line 407

def create_sink name, destination: nil, filter: nil, version: :unspecified
  version = Sink::VERSIONS[version] if Sink::VERSIONS[version]
  ensure_connection!
  resp = connection.create_sink name, destination, filter, version
  if resp.success?
    Sink.from_gapi resp.data, connection
  else
    fail ApiError.from_response(resp)
  end
end

#delete_log(name) ⇒ Boolean

Deletes a log and all its log entries. The log will reappear if it receives new entries.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
logging.delete_log "my-log"

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

def delete_log name
  ensure_connection!
  resp = connection.delete_log name
  if resp.success?
    true
  else
    fail ApiError.from_response(resp)
  end
end

#entries(projects: nil, filter: nil, order: nil, token: nil, max: nil) ⇒ Array<Gcloud::Logging::Entry> Also known as: find_entries

Lists log entries. Use this method to retrieve log entries from Cloud Logging.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
entries = logging.entries
entries.each do |entry|
  puts entry.name
end

With pagination: (See Entry::List)

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
entries = logging.entries
loop do
  entries.each do |entry|
    puts entry.name
  end
  break unless entries.next?
  entries = entries.next
end

123
124
125
126
127
128
129
130
131
132
# File 'lib/gcloud/logging/project.rb', line 123

def entries projects: nil, filter: nil, order: nil, token: nil, max: nil
  ensure_connection!
  resp = connection.list_entries projects: projects, filter: filter,
                                 order: order, token: token, max: max
  if resp.success?
    Entry::List.from_response resp, connection
  else
    fail ApiError.from_response(resp)
  end
end

#entryGcloud::Logging::Entry Also known as: new_entry

Creates an new Entry object to be populated.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging

new_entry = logging.entry.tap do |e|
  e.log_name = "syslog"
  e.resource.type = "cloudsql_database"
  e.timestamp = Time.now
  e.severity = "INFO"
  e.payload = "Export completed"
end

logging.write_entries entry

156
157
158
# File 'lib/gcloud/logging/project.rb', line 156

def entry
  Entry.new
end

#logger(log_name, resource, labels = {}) ⇒ Gcloud::Logging::Logger

Creates a logger object that is API compatible with ruby's standard library Logger.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging

resource = logging.resource "gae_app",
                            module_id: "1",
                            version_id: "20150925t173233"

logger = logging.logger "syslog", resource, env: :production

229
230
231
# File 'lib/gcloud/logging/project.rb', line 229

def logger log_name, resource, labels = {}
  Logger.new self, log_name, resource, labels
end

#metric(name) ⇒ Gcloud::Logging::Metric? Also known as: get_metric, find_metric

Retrieves metric by name.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
metric = logging.metric "existing-metric"

By default nil will be returned if the metric does not exist.

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
metric = logging.metric "non-existing-metric" #=> nil

552
553
554
555
556
557
558
# File 'lib/gcloud/logging/project.rb', line 552

def metric name
  ensure_connection!
  resp = connection.get_metric name
  return Metric.from_gapi(resp.data, connection) if resp.success?
  return nil if resp.status == 404
  fail ApiError.from_response(resp)
end

#metrics(token: nil, max: nil) ⇒ Array<Gcloud::Logging::Metric> Also known as: find_metrics

Retrieves the list of metrics belonging to the project.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
metrics = logging.metrics
metrics.each do |metric|
  puts metric.name
end

With pagination: (See Metric::List)

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
metrics = logging.metrics
loop do
  metrics.each do |metric|
    puts metric.name
  end
  break unless metrics.next?
  metrics = metrics.next
end

485
486
487
488
489
490
491
492
493
# File 'lib/gcloud/logging/project.rb', line 485

def metrics token: nil, max: nil
  ensure_connection!
  resp = connection.list_metrics token: token, max: max
  if resp.success?
    Metric::List.from_response resp, connection
  else
    fail ApiError.from_response(resp)
  end
end

#projectObject

The Logging project connected to.

Examples:

require "gcloud"

gcloud = Gcloud.new "my-todo-project",
                    "/path/to/keyfile.json"
logging = gcloud.logging

logging.project #=> "my-todo-project"

66
67
68
# File 'lib/gcloud/logging/project.rb', line 66

def project
  connection.project
end

#resource(type, labels = {}) ⇒ Gcloud::Logging::Resource Also known as: new_resource

Creates a new Resource object.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging

resource = logging.resource "gae_app",
                            "module_id" => "1",
                            "version_id" => "20150925t173233"

324
325
326
327
328
329
# File 'lib/gcloud/logging/project.rb', line 324

def resource type, labels = {}
  Resource.new.tap do |r|
    r.type = type
    r.labels = labels
  end
end

#resource_descriptors(token: nil, max: nil) ⇒ Array<Gcloud::Logging::ResourceDescriptor> Also known as: find_resource_descriptors

Retrieves the list of monitored resource descriptors that are used by Google Cloud Logging.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
resource_descriptors = logging.resource_descriptors
resource_descriptors.each do |resource_descriptor|
  puts resource_descriptor.name
end

Pagination: (See ResourceDescriptor::List)

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
resource_descriptors = logging.resource_descriptors
loop do
  resource_descriptors.each do |resource_descriptor|
    puts resource_descriptor.name
  end
  break unless resource_descriptors.next?
  resource_descriptors = resource_descriptors.next
end

298
299
300
301
302
303
304
305
306
# File 'lib/gcloud/logging/project.rb', line 298

def resource_descriptors token: nil, max: nil
  ensure_connection!
  resp = connection.list_resource_descriptors token: token, max: max
  if resp.success?
    ResourceDescriptor::List.from_response resp, connection
  else
    fail ApiError.from_response(resp)
  end
end

#sink(sink_name) ⇒ Gcloud::Logging::Sink? Also known as: get_sink, find_sink

Retrieves sink by name.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
sink = logging.sink "existing-sink"

By default nil will be returned if the sink does not exist.

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
sink = logging.sink "non-existing-sink" #=> nil

441
442
443
444
445
446
447
# File 'lib/gcloud/logging/project.rb', line 441

def sink sink_name
  ensure_connection!
  resp = connection.get_sink sink_name
  return Sink.from_gapi(resp.data, connection) if resp.success?
  return nil if resp.status == 404
  fail ApiError.from_response(resp)
end

#sinks(token: nil, max: nil) ⇒ Array<Gcloud::Logging::Sink> Also known as: find_sinks

Retrieves the list of sinks belonging to the project.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
sinks = logging.sinks
sinks.each do |sink|
  puts sink.name
end

With pagination: (See Sink::List)

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging
sinks = logging.sinks
loop do
  sinks.each do |sink|
    puts sink.name
  end
  break unless sinks.next?
  sinks = sinks.next
end

366
367
368
369
370
371
372
373
374
# File 'lib/gcloud/logging/project.rb', line 366

def sinks token: nil, max: nil
  ensure_connection!
  resp = connection.list_sinks token: token, max: max
  if resp.success?
    Sink::List.from_response resp, connection
  else
    fail ApiError.from_response(resp)
  end
end

#write_entries(entries, log_name: nil, resource: nil, labels: nil) ⇒ Boolean

Lists log entries. Use this method to retrieve log entries from Cloud Logging.

Examples:

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging

new_entry = logging.entry.tap do |e|
  e.log_name = "syslog"
  e.resource.type = "cloudsql_database"
  e.timestamp = Time.now
  e.severity = "INFO"
  e.payload = "Export completed"
end

logging.write_entries new_entry

You can provide log name for all entries.

require "gcloud"

gcloud = Gcloud.new
logging = gcloud.logging

logging.write_entries [entry1, entry2], log_name: "syslog"

194
195
196
197
198
199
200
201
# File 'lib/gcloud/logging/project.rb', line 194

def write_entries entries, log_name: nil, resource: nil, labels: nil
  ensure_connection!
  resp = connection.write_entries Array(entries).map(&:to_gapi),
                                  log_name: log_name,
                                  resource: resource, labels: labels
  return true if resp.success?
  fail ApiError.from_response(resp)
end