google-cloud-logging 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/google-cloud-logging.rb +119 -0
- data/lib/google/cloud/logging.rb +293 -0
- data/lib/google/cloud/logging/credentials.rb +31 -0
- data/lib/google/cloud/logging/entry.rb +469 -0
- data/lib/google/cloud/logging/entry/http_request.rb +145 -0
- data/lib/google/cloud/logging/entry/list.rb +180 -0
- data/lib/google/cloud/logging/entry/operation.rb +92 -0
- data/lib/google/cloud/logging/logger.rb +309 -0
- data/lib/google/cloud/logging/metric.rb +172 -0
- data/lib/google/cloud/logging/metric/list.rb +175 -0
- data/lib/google/cloud/logging/project.rb +650 -0
- data/lib/google/cloud/logging/resource.rb +86 -0
- data/lib/google/cloud/logging/resource_descriptor.rb +139 -0
- data/lib/google/cloud/logging/resource_descriptor/list.rb +179 -0
- data/lib/google/cloud/logging/service.rb +270 -0
- data/lib/google/cloud/logging/sink.rb +230 -0
- data/lib/google/cloud/logging/sink/list.rb +173 -0
- data/lib/google/cloud/logging/v2.rb +17 -0
- data/lib/google/cloud/logging/v2/config_service_v2_api.rb +331 -0
- data/lib/google/cloud/logging/v2/config_service_v2_client_config.json +53 -0
- data/lib/google/cloud/logging/v2/logging_service_v2_api.rb +351 -0
- data/lib/google/cloud/logging/v2/logging_service_v2_client_config.json +57 -0
- data/lib/google/cloud/logging/v2/metrics_service_v2_api.rb +330 -0
- data/lib/google/cloud/logging/v2/metrics_service_v2_client_config.json +53 -0
- data/lib/google/cloud/logging/version.rb +22 -0
- data/lib/google/logging/v2/log_entry_pb.rb +44 -0
- data/lib/google/logging/v2/logging_config_pb.rb +59 -0
- data/lib/google/logging/v2/logging_config_services_pb.rb +52 -0
- data/lib/google/logging/v2/logging_metrics_pb.rb +51 -0
- data/lib/google/logging/v2/logging_metrics_services_pb.rb +51 -0
- data/lib/google/logging/v2/logging_pb.rb +59 -0
- data/lib/google/logging/v2/logging_services_pb.rb +56 -0
- metadata +260 -0
@@ -0,0 +1,145 @@
|
|
1
|
+
# Copyright 2016 Google Inc. All rights reserved.
|
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
|
+
|
16
|
+
module Google
|
17
|
+
module Cloud
|
18
|
+
module Logging
|
19
|
+
class Entry
|
20
|
+
##
|
21
|
+
# # Http Request
|
22
|
+
#
|
23
|
+
# HTTP request data associated with a log entry.
|
24
|
+
#
|
25
|
+
# See also {Google::Cloud::Logging::Entry#http_request}.
|
26
|
+
#
|
27
|
+
class HttpRequest
|
28
|
+
##
|
29
|
+
# @private Create an empty HttpRequest object.
|
30
|
+
def initialize
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# The request method. Examples: `"GET"`, `"HEAD"`, `"PUT"`, `"POST"`.
|
35
|
+
# (String)
|
36
|
+
attr_accessor :method
|
37
|
+
|
38
|
+
##
|
39
|
+
# The URL. The scheme (http, https), the host name, the path and the
|
40
|
+
# query portion of the URL that was requested. Example:
|
41
|
+
# `"http://example.com/some/info?color=red"`. (String)
|
42
|
+
attr_accessor :url
|
43
|
+
|
44
|
+
##
|
45
|
+
# The size of the HTTP request message in bytes, including the request
|
46
|
+
# headers and the request body. (Integer)
|
47
|
+
attr_accessor :size
|
48
|
+
|
49
|
+
##
|
50
|
+
# The response code indicating the status of response. Examples:
|
51
|
+
# `200`, `404`. (Integer)
|
52
|
+
attr_accessor :status
|
53
|
+
|
54
|
+
##
|
55
|
+
# The size of the HTTP response message sent back to the client, in
|
56
|
+
# bytes, including the response headers and the response body.
|
57
|
+
# (Integer)
|
58
|
+
attr_accessor :response_size
|
59
|
+
|
60
|
+
##
|
61
|
+
# The user agent sent by the client. Example: `"Mozilla/4.0
|
62
|
+
# (compatible; MSIE 6.0; Windows 98; Q312461; .NET CLR 1.0.3705)"`.
|
63
|
+
# (String)
|
64
|
+
attr_accessor :user_agent
|
65
|
+
|
66
|
+
##
|
67
|
+
# The IP address (IPv4 or IPv6) of the client that issued the HTTP
|
68
|
+
# request. Examples: `"192.168.1.1"`, `"FE80::0202:B3FF:FE1E:8329"`.
|
69
|
+
# (String)
|
70
|
+
attr_accessor :remote_ip
|
71
|
+
|
72
|
+
##
|
73
|
+
# The referer URL of the request, as defined in [HTTP/1.1 Header Field
|
74
|
+
# Definitions](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html).
|
75
|
+
# (String)
|
76
|
+
attr_accessor :referer
|
77
|
+
|
78
|
+
##
|
79
|
+
# Whether an entity was served from cache (with or without
|
80
|
+
# validation). (Boolean)
|
81
|
+
attr_accessor :cache_hit
|
82
|
+
|
83
|
+
##
|
84
|
+
# Whether the response was validated with the origin server before
|
85
|
+
# being served from cache. This field is only meaningful if
|
86
|
+
# `cache_hit` is `true`. (Boolean)
|
87
|
+
attr_accessor :validated
|
88
|
+
|
89
|
+
##
|
90
|
+
# @private Determines if the HttpRequest has any data.
|
91
|
+
def empty?
|
92
|
+
method.nil? &&
|
93
|
+
url.nil? &&
|
94
|
+
size.nil? &&
|
95
|
+
status.nil? &&
|
96
|
+
response_size.nil? &&
|
97
|
+
user_agent.nil? &&
|
98
|
+
remote_ip.nil? &&
|
99
|
+
referer.nil? &&
|
100
|
+
cache_hit.nil? &&
|
101
|
+
validated.nil?
|
102
|
+
end
|
103
|
+
|
104
|
+
##
|
105
|
+
# @private Exports the HttpRequest to a
|
106
|
+
# Google::Logging::Type::HttpRequest object.
|
107
|
+
def to_grpc
|
108
|
+
return nil if empty?
|
109
|
+
Google::Logging::Type::HttpRequest.new(
|
110
|
+
request_method: method.to_s,
|
111
|
+
request_url: url.to_s,
|
112
|
+
request_size: size.to_i,
|
113
|
+
status: status.to_i,
|
114
|
+
response_size: response_size.to_i,
|
115
|
+
user_agent: user_agent.to_s,
|
116
|
+
remote_ip: remote_ip.to_s,
|
117
|
+
referer: referer.to_s,
|
118
|
+
cache_hit: !(!cache_hit),
|
119
|
+
cache_validated_with_origin_server: !(!validated)
|
120
|
+
)
|
121
|
+
end
|
122
|
+
|
123
|
+
##
|
124
|
+
# @private New HttpRequest from a Google::Logging::Type::HttpRequest
|
125
|
+
# object.
|
126
|
+
def self.from_grpc grpc
|
127
|
+
return new if grpc.nil?
|
128
|
+
new.tap do |h|
|
129
|
+
h.method = grpc.request_method
|
130
|
+
h.url = grpc.request_url
|
131
|
+
h.size = grpc.request_size
|
132
|
+
h.status = grpc.status
|
133
|
+
h.response_size = grpc.response_size
|
134
|
+
h.user_agent = grpc.user_agent
|
135
|
+
h.remote_ip = grpc.remote_ip
|
136
|
+
h.referer = grpc.referer
|
137
|
+
h.cache_hit = grpc.cache_hit
|
138
|
+
h.validated = grpc.cache_validated_with_origin_server
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
# Copyright 2016 Google Inc. All rights reserved.
|
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
|
+
|
16
|
+
require "delegate"
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module Logging
|
21
|
+
class Entry
|
22
|
+
##
|
23
|
+
# Entry::List is a special case Array with additional values.
|
24
|
+
class List < DelegateClass(::Array)
|
25
|
+
##
|
26
|
+
# If not empty, indicates that there are more records that match
|
27
|
+
# the request and this value should be passed to continue.
|
28
|
+
attr_accessor :token
|
29
|
+
|
30
|
+
##
|
31
|
+
# @private Create a new Entry::List with an array of Entry instances.
|
32
|
+
def initialize arr = []
|
33
|
+
super arr
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# Whether there is a next page of entries.
|
38
|
+
#
|
39
|
+
# @return [Boolean]
|
40
|
+
#
|
41
|
+
# @example
|
42
|
+
# require "google/cloud"
|
43
|
+
#
|
44
|
+
# gcloud = Google::Cloud.new
|
45
|
+
# logging = gcloud.logging
|
46
|
+
#
|
47
|
+
# entries = logging.entries
|
48
|
+
# if entries.next?
|
49
|
+
# next_entries = entries.next
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
def next?
|
53
|
+
!token.nil?
|
54
|
+
end
|
55
|
+
|
56
|
+
##
|
57
|
+
# Retrieve the next page of entries.
|
58
|
+
#
|
59
|
+
# @return [Sink::List]
|
60
|
+
#
|
61
|
+
# @example
|
62
|
+
# require "google/cloud"
|
63
|
+
#
|
64
|
+
# gcloud = Google::Cloud.new
|
65
|
+
# logging = gcloud.logging
|
66
|
+
#
|
67
|
+
# entries = dataset.entries
|
68
|
+
# if entries.next?
|
69
|
+
# next_entries = entries.next
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
def next
|
73
|
+
return nil unless next?
|
74
|
+
ensure_service!
|
75
|
+
grpc = @service.list_entries token: token, projects: @projects,
|
76
|
+
filter: @filter, order: @order,
|
77
|
+
max: @max
|
78
|
+
self.class.from_grpc grpc, @service
|
79
|
+
end
|
80
|
+
|
81
|
+
##
|
82
|
+
# Retrieves all log entries by repeatedly loading {#next} until
|
83
|
+
# {#next?} returns `false`. Calls the given block once for each log
|
84
|
+
# entry, which is passed as the parameter.
|
85
|
+
#
|
86
|
+
# An Enumerator is returned if no block is given.
|
87
|
+
#
|
88
|
+
# This method may make several API calls until all log entries are
|
89
|
+
# retrieved. Be sure to use as narrow a search criteria as possible.
|
90
|
+
# Please use with caution.
|
91
|
+
#
|
92
|
+
# @param [Integer] request_limit The upper limit of API requests to
|
93
|
+
# make to load all log entries. Default is no limit.
|
94
|
+
# @yield [entry] The block for accessing each log entry.
|
95
|
+
# @yieldparam [Entry] entry The log entry object.
|
96
|
+
#
|
97
|
+
# @return [Enumerator]
|
98
|
+
#
|
99
|
+
# @example Iterating each log entry by passing a block:
|
100
|
+
# require "google/cloud"
|
101
|
+
#
|
102
|
+
# gcloud = Google::Cloud.new
|
103
|
+
# logging = gcloud.logging
|
104
|
+
# entries = logging.entries order: "timestamp desc"
|
105
|
+
#
|
106
|
+
# entries.all do |entry|
|
107
|
+
# puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
|
108
|
+
# end
|
109
|
+
#
|
110
|
+
# @example Using the enumerator by not passing a block:
|
111
|
+
# require "google/cloud"
|
112
|
+
#
|
113
|
+
# gcloud = Google::Cloud.new
|
114
|
+
# logging = gcloud.logging
|
115
|
+
# entries = logging.entries order: "timestamp desc"
|
116
|
+
#
|
117
|
+
# all_payloads = entries.all.map do |entry|
|
118
|
+
# entry.payload
|
119
|
+
# end
|
120
|
+
#
|
121
|
+
# @example Limit the number of API calls made:
|
122
|
+
# require "google/cloud"
|
123
|
+
#
|
124
|
+
# gcloud = Google::Cloud.new
|
125
|
+
# logging = gcloud.logging
|
126
|
+
# entries = logging.entries order: "timestamp desc"
|
127
|
+
#
|
128
|
+
# entries.all(request_limit: 10) do |entry|
|
129
|
+
# puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
|
130
|
+
# end
|
131
|
+
#
|
132
|
+
def all request_limit: nil
|
133
|
+
request_limit = request_limit.to_i if request_limit
|
134
|
+
unless block_given?
|
135
|
+
return enum_for(:all, request_limit: request_limit)
|
136
|
+
end
|
137
|
+
results = self
|
138
|
+
loop do
|
139
|
+
results.each { |r| yield r }
|
140
|
+
if request_limit
|
141
|
+
request_limit -= 1
|
142
|
+
break if request_limit < 0
|
143
|
+
end
|
144
|
+
break unless results.next?
|
145
|
+
results = results.next
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
##
|
150
|
+
# @private New Entry::List from a
|
151
|
+
# Google::Logging::V2::ListLogEntryResponse object.
|
152
|
+
def self.from_grpc grpc_list, service, projects: nil, filter: nil,
|
153
|
+
order: nil, max: nil
|
154
|
+
entries = new(Array(grpc_list.entries).map do |grpc_entry|
|
155
|
+
Entry.from_grpc grpc_entry
|
156
|
+
end)
|
157
|
+
token = grpc_list.next_page_token
|
158
|
+
token = nil if token == ""
|
159
|
+
entries.instance_variable_set "@token", token
|
160
|
+
entries.instance_variable_set "@service", service
|
161
|
+
entries.instance_variable_set "@projects", projects
|
162
|
+
entries.instance_variable_set "@filter", filter
|
163
|
+
entries.instance_variable_set "@order", order
|
164
|
+
entries.instance_variable_set "@max", max
|
165
|
+
entries
|
166
|
+
end
|
167
|
+
|
168
|
+
protected
|
169
|
+
|
170
|
+
##
|
171
|
+
# @private Raise an error unless an active connection to the service
|
172
|
+
# is available.
|
173
|
+
def ensure_service!
|
174
|
+
fail "Must have active connection to service" unless @service
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# Copyright 2016 Google Inc. All rights reserved.
|
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
|
+
|
16
|
+
module Google
|
17
|
+
module Cloud
|
18
|
+
module Logging
|
19
|
+
class Entry
|
20
|
+
##
|
21
|
+
# # Operation
|
22
|
+
#
|
23
|
+
# Additional information about a potentially long-running operation with
|
24
|
+
# which a log entry is associated.
|
25
|
+
#
|
26
|
+
# See also {Google::Cloud::Logging::Entry#operation}.
|
27
|
+
#
|
28
|
+
class Operation
|
29
|
+
##
|
30
|
+
# @private Create an empty Operation object.
|
31
|
+
def initialize
|
32
|
+
end
|
33
|
+
|
34
|
+
##
|
35
|
+
# An arbitrary operation identifier. Log entries with the same
|
36
|
+
# identifier are assumed to be part of the same operation.
|
37
|
+
attr_accessor :id
|
38
|
+
|
39
|
+
##
|
40
|
+
# An arbitrary producer identifier. The combination of `id` and
|
41
|
+
# `producer` must be globally unique. Examples for `producer`:
|
42
|
+
# `"MyDivision.MyBigCompany.com"`,
|
43
|
+
# `"github.com/MyProject/MyApplication"`.
|
44
|
+
attr_accessor :producer
|
45
|
+
|
46
|
+
##
|
47
|
+
# Set this to `true` if this is the first log entry in the operation.
|
48
|
+
attr_accessor :first
|
49
|
+
|
50
|
+
##
|
51
|
+
# Set this to `true` if this is the last log entry in the operation.
|
52
|
+
attr_accessor :last
|
53
|
+
|
54
|
+
##
|
55
|
+
# @private Determines if the Operation has any data.
|
56
|
+
def empty?
|
57
|
+
id.nil? &&
|
58
|
+
producer.nil? &&
|
59
|
+
first.nil? &&
|
60
|
+
last.nil?
|
61
|
+
end
|
62
|
+
|
63
|
+
##
|
64
|
+
# @private Exports the Operation to a
|
65
|
+
# Google::Logging::V2::LogEntryOperation object.
|
66
|
+
def to_grpc
|
67
|
+
return nil if empty?
|
68
|
+
Google::Logging::V2::LogEntryOperation.new(
|
69
|
+
id: id.to_s,
|
70
|
+
producer: producer.to_s,
|
71
|
+
first: !(!first),
|
72
|
+
last: !(!last)
|
73
|
+
)
|
74
|
+
end
|
75
|
+
|
76
|
+
##
|
77
|
+
# @private New HttpRequest from a
|
78
|
+
# Google::Logging::V2::LogEntryOperation object.
|
79
|
+
def self.from_grpc grpc
|
80
|
+
return new if grpc.nil?
|
81
|
+
new.tap do |o|
|
82
|
+
o.id = grpc.id
|
83
|
+
o.producer = grpc.producer
|
84
|
+
o.first = grpc.first
|
85
|
+
o.last = grpc.last
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,309 @@
|
|
1
|
+
# Copyright 2016 Google Inc. All rights reserved.
|
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
|
+
|
16
|
+
module Google
|
17
|
+
module Cloud
|
18
|
+
module Logging
|
19
|
+
##
|
20
|
+
# # Logger
|
21
|
+
#
|
22
|
+
# A (mostly) API-compatible logger for ruby's Logger.
|
23
|
+
#
|
24
|
+
# @example
|
25
|
+
# require "google/cloud"
|
26
|
+
#
|
27
|
+
# gcloud = Google::Cloud.new
|
28
|
+
# logging = gcloud.logging
|
29
|
+
#
|
30
|
+
# resource = logging.resource "gae_app",
|
31
|
+
# module_id: "1",
|
32
|
+
# version_id: "20150925t173233"
|
33
|
+
#
|
34
|
+
# logger = logging.logger "my_app_log", resource, env: :production
|
35
|
+
# logger.info "Job started."
|
36
|
+
#
|
37
|
+
class Logger
|
38
|
+
##
|
39
|
+
# @private The logging object.
|
40
|
+
attr_accessor :logging
|
41
|
+
|
42
|
+
##
|
43
|
+
# @private The Google Cloud log_name to write the log entry with.
|
44
|
+
attr_reader :log_name
|
45
|
+
|
46
|
+
##
|
47
|
+
# @private The Google Cloud resource to write the log entry with.
|
48
|
+
attr_reader :resource
|
49
|
+
|
50
|
+
##
|
51
|
+
# @private The Google Cloud labels to write the log entry with.
|
52
|
+
attr_reader :labels
|
53
|
+
|
54
|
+
##
|
55
|
+
# @private Creates a new Logger instance.
|
56
|
+
def initialize logging, log_name, resource, labels = nil
|
57
|
+
@logging = logging
|
58
|
+
@log_name = log_name
|
59
|
+
@resource = resource
|
60
|
+
@labels = labels
|
61
|
+
@level = 0 # DEBUG is the default behavior
|
62
|
+
end
|
63
|
+
|
64
|
+
##
|
65
|
+
# Log a `DEBUG` entry.
|
66
|
+
#
|
67
|
+
# @param [String, Hash] message The log entry payload, represented as
|
68
|
+
# either a string, a hash (JSON), or a hash (protocol buffer).
|
69
|
+
# @yield Evaluates to the message to log. This is not evaluated unless
|
70
|
+
# the logger's level is sufficient to log the message. This allows you
|
71
|
+
# to create potentially expensive logging messages that are only
|
72
|
+
# called when the logger is configured to show them.
|
73
|
+
#
|
74
|
+
def debug message = nil, &block
|
75
|
+
if block_given?
|
76
|
+
add 0, nil, message, &block
|
77
|
+
else
|
78
|
+
add 0, message, nil, &block
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
##
|
83
|
+
# Log an `INFO` entry.
|
84
|
+
#
|
85
|
+
# @param [String, Hash] message The log entry payload, represented as
|
86
|
+
# either a string, a hash (JSON), or a hash (protocol buffer).
|
87
|
+
# @yield Evaluates to the message to log. This is not evaluated unless
|
88
|
+
# the logger's level is sufficient to log the message. This allows you
|
89
|
+
# to create potentially expensive logging messages that are only
|
90
|
+
# called when the logger is configured to show them.
|
91
|
+
#
|
92
|
+
def info message = nil, &block
|
93
|
+
if block_given?
|
94
|
+
add 1, nil, message, &block
|
95
|
+
else
|
96
|
+
add 1, message, nil, &block
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
##
|
101
|
+
# Log a `WARN` entry.
|
102
|
+
#
|
103
|
+
# @param [String, Hash] message The log entry payload, represented as
|
104
|
+
# either a string, a hash (JSON), or a hash (protocol buffer).
|
105
|
+
# @yield Evaluates to the message to log. This is not evaluated unless
|
106
|
+
# the logger's level is sufficient to log the message. This allows you
|
107
|
+
# to create potentially expensive logging messages that are only
|
108
|
+
# called when the logger is configured to show them.
|
109
|
+
#
|
110
|
+
def warn message = nil, &block
|
111
|
+
if block_given?
|
112
|
+
add 2, nil, message, &block
|
113
|
+
else
|
114
|
+
add 2, message, nil, &block
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
##
|
119
|
+
# Log an `ERROR` entry.
|
120
|
+
#
|
121
|
+
# @param [String, Hash] message The log entry payload, represented as
|
122
|
+
# either a string, a hash (JSON), or a hash (protocol buffer).
|
123
|
+
# @yield Evaluates to the message to log. This is not evaluated unless
|
124
|
+
# the logger's level is sufficient to log the message. This allows you
|
125
|
+
# to create potentially expensive logging messages that are only
|
126
|
+
# called when the logger is configured to show them.
|
127
|
+
#
|
128
|
+
def error message = nil, &block
|
129
|
+
if block_given?
|
130
|
+
add 3, nil, message, &block
|
131
|
+
else
|
132
|
+
add 3, message, nil, &block
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
##
|
137
|
+
# Log a `FATAL` entry.
|
138
|
+
#
|
139
|
+
# @param [String, Hash] message The log entry payload, represented as
|
140
|
+
# either a string, a hash (JSON), or a hash (protocol buffer).
|
141
|
+
# @yield Evaluates to the message to log. This is not evaluated unless
|
142
|
+
# the logger's level is sufficient to log the message. This allows you
|
143
|
+
# to create potentially expensive logging messages that are only
|
144
|
+
# called when the logger is configured to show them.
|
145
|
+
#
|
146
|
+
def fatal message = nil, &block
|
147
|
+
if block_given?
|
148
|
+
add 4, nil, message, &block
|
149
|
+
else
|
150
|
+
add 4, message, nil, &block
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
##
|
155
|
+
# Log an `UNKNOWN` entry. This will be printed no matter what the
|
156
|
+
# logger's current severity level is.
|
157
|
+
#
|
158
|
+
# @param [String, Hash] message The log entry payload, represented as
|
159
|
+
# either a string, a hash (JSON), or a hash (protocol buffer).
|
160
|
+
# @yield Evaluates to the message to log. This is not evaluated unless
|
161
|
+
# the logger's level is sufficient to log the message. This allows you
|
162
|
+
# to create potentially expensive logging messages that are only
|
163
|
+
# called when the logger is configured to show them.
|
164
|
+
#
|
165
|
+
def unknown message = nil, &block
|
166
|
+
if block_given?
|
167
|
+
add 5, nil, message, &block
|
168
|
+
else
|
169
|
+
add 5, message, nil, &block
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
##
|
174
|
+
# Log a message if the given severity is high enough. This is the
|
175
|
+
# generic logging method. Users will be more inclined to use {#debug},
|
176
|
+
# {#info}, {#warn}, {#error}, and {#fatal}.
|
177
|
+
#
|
178
|
+
# @param [Integer, String, Symbol] severity the integer code for or the
|
179
|
+
# name of the severity level
|
180
|
+
# @param [String, Hash] message The log entry payload, represented as
|
181
|
+
# either a string, a hash (JSON), or a hash (protocol buffer).
|
182
|
+
# @yield Evaluates to the message to log. This is not evaluated unless
|
183
|
+
# the logger's level is sufficient to log the message. This allows you
|
184
|
+
# to create potentially expensive logging messages that are only
|
185
|
+
# called when the logger is configured to show them.
|
186
|
+
#
|
187
|
+
def add severity, message = nil, progname = nil
|
188
|
+
severity = derive_severity(severity) || 5 # 5 is UNKNOWN/DEFAULT
|
189
|
+
return true if severity < @level
|
190
|
+
|
191
|
+
if message.nil?
|
192
|
+
if block_given?
|
193
|
+
message = yield
|
194
|
+
else
|
195
|
+
message = progname
|
196
|
+
# progname = nil # TODO: Figure out what to do with the progname
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
write_entry severity, message
|
201
|
+
end
|
202
|
+
alias_method :log, :add
|
203
|
+
|
204
|
+
##
|
205
|
+
# Returns `true` if the current severity level allows for sending
|
206
|
+
# `DEBUG` messages.
|
207
|
+
def debug?
|
208
|
+
@level <= 0
|
209
|
+
end
|
210
|
+
|
211
|
+
##
|
212
|
+
# Returns `true` if the current severity level allows for sending `INFO`
|
213
|
+
# messages.
|
214
|
+
def info?
|
215
|
+
@level <= 1
|
216
|
+
end
|
217
|
+
|
218
|
+
##
|
219
|
+
# Returns `true` if the current severity level allows for sending `WARN`
|
220
|
+
# messages.
|
221
|
+
def warn?
|
222
|
+
@level <= 2
|
223
|
+
end
|
224
|
+
|
225
|
+
##
|
226
|
+
# Returns `true` if the current severity level allows for sending
|
227
|
+
# `ERROR` messages.
|
228
|
+
def error?
|
229
|
+
@level <= 3
|
230
|
+
end
|
231
|
+
|
232
|
+
##
|
233
|
+
# Returns `true` if the current severity level allows for sending
|
234
|
+
# `FATAL` messages.
|
235
|
+
def fatal?
|
236
|
+
@level <= 4
|
237
|
+
end
|
238
|
+
|
239
|
+
##
|
240
|
+
# Sets the logging severity level.
|
241
|
+
#
|
242
|
+
# @param [Integer, String, Symbol] severity the integer code for or the
|
243
|
+
# name of the severity level
|
244
|
+
#
|
245
|
+
# @example
|
246
|
+
# require "google/cloud"
|
247
|
+
#
|
248
|
+
# gcloud = Google::Cloud.new
|
249
|
+
# logging = gcloud.logging
|
250
|
+
#
|
251
|
+
# resource = logging.resource "gae_app",
|
252
|
+
# module_id: "1",
|
253
|
+
# version_id: "20150925t173233"
|
254
|
+
#
|
255
|
+
# logger = logging.logger "my_app_log", resource, env: :production
|
256
|
+
#
|
257
|
+
# logger.level = "INFO"
|
258
|
+
# logger.debug "Job started." # No log entry written
|
259
|
+
#
|
260
|
+
def level= severity
|
261
|
+
new_level = derive_severity severity
|
262
|
+
fail ArgumentError, "invalid log level: #{severity}" if new_level.nil?
|
263
|
+
@level = new_level
|
264
|
+
end
|
265
|
+
alias_method :sev_threshold=, :level=
|
266
|
+
|
267
|
+
protected
|
268
|
+
|
269
|
+
##
|
270
|
+
# @private Write a log entry to the Stackdriver Logging service.
|
271
|
+
def write_entry severity, message
|
272
|
+
entry = logging.entry.tap do |e|
|
273
|
+
e.severity = gcloud_severity(severity)
|
274
|
+
e.payload = message
|
275
|
+
end
|
276
|
+
|
277
|
+
logging.write_entries entry, log_name: log_name,
|
278
|
+
resource: resource,
|
279
|
+
labels: labels
|
280
|
+
end
|
281
|
+
|
282
|
+
##
|
283
|
+
# @private Get the logger level number from severity value object.
|
284
|
+
def derive_severity severity
|
285
|
+
return severity if severity.is_a? Integer
|
286
|
+
|
287
|
+
downcase_severity = severity.to_s.downcase
|
288
|
+
case downcase_severity
|
289
|
+
when "debug".freeze then 0
|
290
|
+
when "info".freeze then 1
|
291
|
+
when "warn".freeze then 2
|
292
|
+
when "error".freeze then 3
|
293
|
+
when "fatal".freeze then 4
|
294
|
+
when "unknown".freeze then 5
|
295
|
+
else nil
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
##
|
300
|
+
# @private Get Google Cloud deverity from logger level number.
|
301
|
+
def gcloud_severity severity_int
|
302
|
+
%i(DEBUG INFO WARNING ERROR CRITICAL DEFAULT)[severity_int]
|
303
|
+
rescue
|
304
|
+
:DEFAULT
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
308
|
+
end
|
309
|
+
end
|