Skip to content

Commit c96e94d

Browse files
author
remi Taylor
committed
Initial Stackdriver Logging samples
1 parent bc03749 commit c96e94d

6 files changed

Lines changed: 350 additions & 0 deletions

File tree

logging/Gemfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 writing, 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+
source "https://rubygems.org"
16+
17+
gem "gcloud"
18+
19+
group :test do
20+
gem "rake"
21+
gem "rubocop"
22+
gem "rspec"
23+
gem "rack-test"
24+
end

logging/Gemfile.lock

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
activesupport (4.2.6)
5+
i18n (~> 0.7)
6+
json (~> 1.7, >= 1.7.7)
7+
minitest (~> 5.1)
8+
thread_safe (~> 0.3, >= 0.3.4)
9+
tzinfo (~> 1.1)
10+
addressable (2.4.0)
11+
ast (2.2.0)
12+
autoparse (0.3.3)
13+
addressable (>= 2.3.1)
14+
extlib (>= 0.9.15)
15+
multi_json (>= 1.0.0)
16+
diff-lcs (1.2.5)
17+
digest-crc (0.4.1)
18+
extlib (0.9.16)
19+
faraday (0.9.2)
20+
multipart-post (>= 1.2, < 3)
21+
gcloud (0.9.0)
22+
digest-crc (~> 0.4)
23+
google-api-client (~> 0.8.3)
24+
google-protobuf (~> 3.0.0.alpha.5.0.5)
25+
grpc (~> 0.13.1)
26+
mime-types (~> 2.4)
27+
zonefile (~> 1.04)
28+
google-api-client (0.8.6)
29+
activesupport (>= 3.2)
30+
addressable (~> 2.3)
31+
autoparse (~> 0.3)
32+
extlib (~> 0.9)
33+
faraday (~> 0.9)
34+
googleauth (~> 0.3)
35+
launchy (~> 2.4)
36+
multi_json (~> 1.10)
37+
retriable (~> 1.4)
38+
signet (~> 0.6)
39+
google-protobuf (3.0.0.alpha.5.0.5.1)
40+
googleauth (0.5.1)
41+
faraday (~> 0.9)
42+
jwt (~> 1.4)
43+
logging (~> 2.0)
44+
memoist (~> 0.12)
45+
multi_json (~> 1.11)
46+
os (~> 0.9)
47+
signet (~> 0.7)
48+
grpc (0.13.1)
49+
google-protobuf (~> 3.0.0.alpha.5.0.3)
50+
googleauth (~> 0.5.1)
51+
i18n (0.7.0)
52+
json (1.8.3)
53+
jwt (1.5.4)
54+
launchy (2.4.3)
55+
addressable (~> 2.3)
56+
little-plugger (1.1.4)
57+
logging (2.1.0)
58+
little-plugger (~> 1.1)
59+
multi_json (~> 1.10)
60+
memoist (0.14.0)
61+
mime-types (2.99.2)
62+
minitest (5.9.0)
63+
multi_json (1.12.1)
64+
multipart-post (2.0.0)
65+
os (0.9.6)
66+
parser (2.3.1.1)
67+
ast (~> 2.2)
68+
powerpack (0.1.1)
69+
rack (1.6.4)
70+
rack-test (0.6.3)
71+
rack (>= 1.0)
72+
rainbow (2.1.0)
73+
rake (11.1.2)
74+
retriable (1.4.1)
75+
rspec (3.4.0)
76+
rspec-core (~> 3.4.0)
77+
rspec-expectations (~> 3.4.0)
78+
rspec-mocks (~> 3.4.0)
79+
rspec-core (3.4.4)
80+
rspec-support (~> 3.4.0)
81+
rspec-expectations (3.4.0)
82+
diff-lcs (>= 1.2.0, < 2.0)
83+
rspec-support (~> 3.4.0)
84+
rspec-mocks (3.4.1)
85+
diff-lcs (>= 1.2.0, < 2.0)
86+
rspec-support (~> 3.4.0)
87+
rspec-support (3.4.1)
88+
rubocop (0.40.0)
89+
parser (>= 2.3.1.0, < 3.0)
90+
powerpack (~> 0.1)
91+
rainbow (>= 1.99.1, < 3.0)
92+
ruby-progressbar (~> 1.7)
93+
unicode-display_width (~> 1.0, >= 1.0.1)
94+
ruby-progressbar (1.8.1)
95+
signet (0.7.2)
96+
addressable (~> 2.3)
97+
faraday (~> 0.9)
98+
jwt (~> 1.5)
99+
multi_json (~> 1.10)
100+
thread_safe (0.3.5)
101+
tzinfo (1.2.2)
102+
thread_safe (~> 0.1)
103+
unicode-display_width (1.0.5)
104+
zonefile (1.04)
105+
106+
PLATFORMS
107+
ruby
108+
109+
DEPENDENCIES
110+
gcloud
111+
rack-test
112+
rake
113+
rspec
114+
rubocop
115+
116+
BUNDLED WITH
117+
1.12.5

logging/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Stackdriver Logging Sample
2+
3+
This sample provides example code for
4+
[cloud.google.com/logging/docs](https://cloud.google.com/logging/docs).
5+
6+
## Setup
7+
8+
Before you can run or test the sample, you will need to enable the Stackdriver Logging API in the [Google Developers Console](https://console.developers.google.com/projectselector/apis/api/datastore/overview).
9+
10+
## Testing
11+
12+
The tests for the sample are integration tests that run against the Logging
13+
service and require authentication.
14+
15+
### Authenticating
16+
17+
Set one of the following environment variables to your Google Cloud Platform
18+
project ID:
19+
20+
* `GCLOUD_PROJECT`
21+
* `GOOGLE_CLOUD_PROJECT`
22+
23+
For more information, see
24+
[Authentication](https://googlecloudplatform.github.io/gcloud-ruby/#/docs/guides/authentication).
25+
26+
### Running the tests
27+
28+
```bash
29+
$ bundle exec rake spec
30+
```
31+

logging/Rakefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 writing, 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+
require "rspec/core/rake_task"
16+
RSpec::Core::RakeTask.new(:spec)
17+
18+
require "rubocop/rake_task"
19+
RuboCop::RakeTask.new
20+
21+
task default: :spec

logging/sample.rb

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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

logging/spec/sample_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 writing, 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+
require_relative "../sample"
16+
require "rspec"
17+
18+
describe "Logging sample" do
19+
end

0 commit comments

Comments
 (0)