google-cloud-logging 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ed947d4ac0a7d053284364df99d1d27eb2dd013
4
- data.tar.gz: 66c8792bbad62413185d1abfedb3e3e9692def55
3
+ metadata.gz: 4c12b20d76a762cc4d57a454cc8464d4e301f31a
4
+ data.tar.gz: 36cda33c662c40a95bafff6068adaad0b7c1df71
5
5
  SHA512:
6
- metadata.gz: e49e794f719c33b0c7d5473610152544e0b17519da2bc04bf7543a6c973c931d56c336a5b9fee74bec6781f123ba5b72ee6be23ac2f78abbdea5e5fdefe1dfd7
7
- data.tar.gz: a63f6b59880746e4898a9e5dd32b72dbbd866be2dcf0801fff66e9a151d7fe3b3c7d4edbc37b01ea634939bd34b07c6cadd7abb2648a11cf2f4eaf4a255829b1
6
+ metadata.gz: 1bb3bbec95098e11bb58fe87c41edbb93f8b8fb36dadfbe627434fdf7540e4f1dab64d79215d6da4e97b280fc15ac923cc686e53b3a3c6f7aba0f5a657095148
7
+ data.tar.gz: '059dfed7ab14d14903c65824489e968b001947e7b13be4671aa69b9719ab3c7e701c10d7cce1bed08c20c8b787d93af102c1eb583174c92ea8654b47b828155f'
@@ -19,6 +19,11 @@ module Google
19
19
  module Cloud
20
20
  module Logging
21
21
  class Middleware
22
+ ##
23
+ # The default log name used to instantiate the default logger if one
24
+ # isn't provided.
25
+ DEFAULT_LOG_NAME = "ruby_app_log"
26
+
22
27
  ##
23
28
  # A default value for the log_name_map argument. Directs health check
24
29
  # logs to a separate log name so they don't spam the main log.
@@ -35,7 +40,16 @@ module Google
35
40
  # @param [Google::Cloud::Logging::Logger] logger A logger to be used by
36
41
  # this middleware. The middleware will be interacting with the logger
37
42
  # to track Stackdriver request trace ID. It also properly sets
38
- # env["rack.logger"] to this assigned logger for accessing.
43
+ # env["rack.logger"] to this assigned logger for accessing. If not
44
+ # specified, a default logger with be used.
45
+ # @param [String] project_id Project identifier for the Stackdriver
46
+ # Logging service. Used to create a default logger if one isn't
47
+ # specified and a service account is used for authentication.
48
+ # Optional.
49
+ # @param [String, Hash] keyfile Keyfile downloaded from Google Cloud. If
50
+ # file path the file must be readable. Used to create a default logger
51
+ # if one isn't specified and a service account is used for
52
+ # authentication. Optional.
39
53
  # @param [Hash] log_name_map A map from URI path to log name override.
40
54
  # The path may be a string or regex. If a request path matches an
41
55
  # entry in this map, the log name is set accordingly, otherwise the
@@ -44,9 +58,17 @@ module Google
44
58
  # @return [Google::Cloud::Logging::Middleware] A new
45
59
  # Google::Cloud::Logging::Middleware instance
46
60
  #
47
- def initialize app, logger: nil, log_name_map: DEFAULT_LOG_NAME_MAP
61
+ def initialize app, logger: nil, project_id: nil, keyfile: nil,
62
+ log_name_map: DEFAULT_LOG_NAME_MAP
48
63
  @app = app
49
- @logger = logger
64
+ if logger
65
+ @logger = logger
66
+ else
67
+ logging = Google::Cloud::Logging.new project: project_id,
68
+ keyfile: keyfile
69
+ resource = Middleware.build_monitored_resource
70
+ @logger = logging.logger DEFAULT_LOG_NAME, resource
71
+ end
50
72
  @log_name_map = log_name_map
51
73
  end
52
74
 
@@ -19,7 +19,7 @@ module Google
19
19
  module Logging
20
20
  ##
21
21
  # Default log name to be used for Stackdriver Logging
22
- DEFAULT_LOG_NAME = "ruby_app_log"
22
+ DEFAULT_LOG_NAME = Middleware::DEFAULT_LOG_NAME
23
23
 
24
24
  ##
25
25
  # Railtie
@@ -65,20 +65,19 @@ module Google
65
65
 
66
66
  initializer "Stackdriver.Logging", before: :initialize_logger do |app|
67
67
  if self.class.use_logging? app.config
68
- gcp_config = app.config.google_cloud
69
- log_config = gcp_config.logging
68
+ logging_config = Railtie.parse_rails_config config
70
69
 
71
- project_id = log_config.project_id || gcp_config.project_id
72
- keyfile = log_config.keyfile || gcp_config.keyfile
73
- resource_type = log_config.monitored_resource.type
74
- resource_labels = log_config.monitored_resource.labels
70
+ project_id = logging_config[:project_id]
71
+ keyfile = logging_config[:keyfile]
72
+ resource_type = logging_config[:resource_type]
73
+ resource_labels = logging_config[:resource_labels]
74
+ log_name = logging_config[:log_name]
75
75
 
76
76
  logging = Google::Cloud::Logging.new project: project_id,
77
77
  keyfile: keyfile
78
78
  resource =
79
79
  Logging::Middleware.build_monitored_resource resource_type,
80
80
  resource_labels
81
- log_name = log_config.log_name || DEFAULT_LOG_NAME
82
81
 
83
82
  app.config.logger = logging.logger log_name, resource
84
83
  app.middleware.insert_before Rails::Rack::Logger,
@@ -109,34 +108,54 @@ module Google
109
108
  # @return [Boolean] Whether to use Stackdriver Logging
110
109
  #
111
110
  def self.use_logging? config
112
- gcp_config = config.google_cloud
111
+ logging_config = Railtie.parse_rails_config config
112
+
113
113
  # Return false if config.google_cloud.use_logging is explicitly false
114
- return false if gcp_config.key?(:use_logging) &&
115
- !gcp_config.use_logging
114
+ use_logging = logging_config[:use_logging]
115
+ return false if use_logging == false
116
+
117
+ project_id = logging_config[:project_id] ||
118
+ Google::Cloud::Logging::Project.default_project
119
+ keyfile = logging_config[:keyfile]
116
120
 
117
121
  # Try authenticate authorize client API. Return false if unable to
118
122
  # authorize.
119
- keyfile = gcp_config.logging.keyfile || gcp_config.keyfile
120
123
  begin
121
124
  Google::Cloud::Logging::Credentials.credentials_with_scope keyfile
122
125
  rescue Exception => e
123
- warn "Google::Cloud::Logging is not activated due to " \
124
- "authorization error: #{e.message}\nFalling back to default " \
125
- "logger"
126
+ STDOUT.puts "Note: Google::Cloud::Logging is disabled because " \
127
+ "it failed to authorize with the service. (#{e.message}) " \
128
+ "Falling back to the default Rails logger."
126
129
  return false
127
130
  end
128
131
 
129
- project_id = gcp_config.logging.project_id || gcp_config.project_id ||
130
- Google::Cloud::Logging::Project.default_project
131
132
  if project_id.to_s.empty?
132
- warn "Google::Cloud::Logging is not activated due to empty " \
133
- "project_id; falling back to default logger"
133
+ STDOUT.puts "Note: Google::Cloud::Logging is disabled because " \
134
+ "the project ID could not be determined. " \
135
+ "Falling back to the default Rails logger."
134
136
  return false
135
137
  end
136
138
 
137
139
  # Otherwise default to true if Rails is running in production or
138
140
  # config.google_cloud.use_logging is true
139
- Rails.env.production? || gcp_config.use_logging
141
+ Rails.env.production? || use_logging
142
+ end
143
+
144
+ ##
145
+ # @private Helper method to parse rails config into a flattened hash
146
+ def self.parse_rails_config config
147
+ gcp_config = config.google_cloud
148
+ logging_config = gcp_config.logging
149
+ use_logging =
150
+ gcp_config.key?(:use_logging) ? gcp_config.use_logging : nil
151
+ {
152
+ project_id: logging_config.project_id || gcp_config.project_id,
153
+ keyfile: logging_config.keyfile || gcp_config.keyfile,
154
+ resource_type: logging_config.monitored_resource.type,
155
+ resource_labels: logging_config.monitored_resource.labels,
156
+ log_name: logging_config.log_name || Middleware::DEFAULT_LOG_NAME,
157
+ use_logging: use_logging
158
+ }
140
159
  end
141
160
  end
142
161
  end
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Logging
19
- VERSION = "1.0.0"
19
+ VERSION = "1.0.1"
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-04-01 00:00:00.000000000 Z
12
+ date: 2017-04-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: google-cloud-core