|
| 1 | +# Copyright 2016 Google, Inc |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in write, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +def create_logging_client |
| 16 | + # [START create_logging_client] |
| 17 | + require "gcloud" |
| 18 | + |
| 19 | + gcloud = Gcloud.new "my-gcp-project-id" |
| 20 | + logging = gcloud.logging |
| 21 | + # [END create_logging_client] |
| 22 | +end |
| 23 | + |
| 24 | +def list_log_sinks |
| 25 | + # [START list_log_sinks] |
| 26 | + require "gcloud" |
| 27 | + |
| 28 | + gcloud = Gcloud.new "my-gcp-project-id" |
| 29 | + logging = gcloud.logging |
| 30 | + |
| 31 | + logging.sinks.each do |sink| |
| 32 | + puts "#{sink.name}: #{sink.filter} -> #{sink.destination}" |
| 33 | + end |
| 34 | + # [END list_log_sinks] |
| 35 | +end |
| 36 | + |
| 37 | +def create_log_sink |
| 38 | + # [START create_log_sink] |
| 39 | + require "gcloud" |
| 40 | + |
| 41 | + gcloud = Gcloud.new "my-gcp-project-id" |
| 42 | + logging = gcloud.logging |
| 43 | + storage = gcloud.storage |
| 44 | + bucket = storage.create_bucket "my-logs-bucket" |
| 45 | + |
| 46 | + # Grant owner permission to Cloud Logging service |
| 47 | + email = "cloud-logs@google.com" |
| 48 | + bucket.acl.add_owner "group-#{email}" |
| 49 | + |
| 50 | + sink = logging.create_sink "my-sink", "storage.googleapis.com/#{bucket.id}" |
| 51 | + # [END create_log_sink] |
| 52 | +end |
| 53 | + |
| 54 | +def update_log_sink |
| 55 | + # [START update_log_sink] |
| 56 | + require "gcloud" |
| 57 | + |
| 58 | + gcloud = Gcloud.new "my-gcp-project-id" |
| 59 | + logging = gcloud.logging |
| 60 | + storage = gcloud.storage |
| 61 | + bucket = storage.bucket "my-logs-bucket" |
| 62 | + |
| 63 | + sink = logging.sink "my-sink" |
| 64 | + sink.destination = "storage.googleapis.com/#{bucket.id}" |
| 65 | + sink.save |
| 66 | + # [END update_log_sink] |
| 67 | +end |
| 68 | + |
| 69 | +def delete_log_sink |
| 70 | + # [START delete_log_sink] |
| 71 | + require "gcloud" |
| 72 | + |
| 73 | + gcloud = Gcloud.new "my-gcp-project-id" |
| 74 | + logging = gcloud.logging |
| 75 | + |
| 76 | + sink = logging.sink "my-sink" |
| 77 | + sink.delete |
| 78 | + # [END delete_log_sink] |
| 79 | +end |
| 80 | + |
| 81 | +def list_log_entries |
| 82 | + # [START list_log_entries] |
| 83 | + require "gcloud" |
| 84 | + |
| 85 | + gcloud = Gcloud.new "my-gcp-project-id" |
| 86 | + logging = gcloud.logging |
| 87 | + entries = logging.entries filter: %{resource.type = "gae_app"} |
| 88 | + |
| 89 | + entries.each do |entry| |
| 90 | + puts "[#{entry.timestamp}] #{entry.log_name} #{entry.payload.inspect}" |
| 91 | + end |
| 92 | + # [END list_log_entries] |
| 93 | +end |
| 94 | + |
| 95 | +def write_log_entry |
| 96 | + # [START write_log_entry] |
| 97 | + require "gcloud" |
| 98 | + |
| 99 | + gcloud = Gcloud.new "my-gcp-project-id" |
| 100 | + logging = gcloud.logging |
| 101 | + |
| 102 | + entry = logging.entry |
| 103 | + entry.log_name = "my_application_log" |
| 104 | + entry.payload = "Log message" |
| 105 | + entry.severity = :NOTICE |
| 106 | + entry.resource.type = "gae_app" |
| 107 | + entry.resource.labels[:module_id] = "default" |
| 108 | + entry.resource.labels[:version_id] = "20160101t163030" |
| 109 | + |
| 110 | + logging.write_entries entry |
| 111 | + # [END write_log_entry] |
| 112 | +end |
| 113 | + |
| 114 | +def delete_log |
| 115 | +# [START delete_log] |
| 116 | + require "gcloud" |
| 117 | + |
| 118 | + gcloud = Gcloud.new "my-gcp-project-id" |
| 119 | + logging = gcloud.logging |
| 120 | + |
| 121 | + logging.delete_log "my_application_log" |
| 122 | +# [END delete_log] |
| 123 | +end |
| 124 | + |
| 125 | +def write_log_entry_using_ruby_logger |
| 126 | + # [START write_log_entry_using_ruby_logger] |
| 127 | + require "gcloud" |
| 128 | + |
| 129 | + gcloud = Gcloud.new "my-gcp-project-id" |
| 130 | + logging = gcloud.logging |
| 131 | + resource = logging.resource "gae_app", module_id: "default", version_id: "20160101t163030" |
| 132 | + logger = logging.logger "my_application_log", resource |
| 133 | + |
| 134 | + logger.info "Log message" |
| 135 | + # [END write_log_entry_using_ruby_logger] |
| 136 | +end |
| 137 | + |
| 138 | +write_log_entry_using_ruby_logger |
0 commit comments