lib/google/cloud/logging.rb in google-cloud-logging-1.4.0 vs lib/google/cloud/logging.rb in google-cloud-logging-1.5.0
- old
+ new
@@ -13,10 +13,12 @@
# limitations under the License.
require "google-cloud-logging"
require "google/cloud/logging/project"
+require "google/cloud/config"
+require "google/cloud/env"
require "stackdriver/core"
module Google
module Cloud
##
@@ -36,15 +38,16 @@
#
# For general information about Stackdriver Logging, read [Stackdriver
# Logging Documentation](https://cloud.google.com/logging/docs/).
#
# The goal of google-cloud is to provide an API that is comfortable to
- # Rubyists. Authentication is handled by {Google::Cloud#logging}. You can
- # provide the project and credential information to connect to the
- # Stackdriver Logging service, or if you are running on Google Compute
- # Engine this configuration is taken care of for you. You can read more
- # about the options for connecting in the [Authentication
+ # Rubyists. Your authentication credentials are detected automatically in
+ # Google Cloud Platform environments such as Google Compute Engine, Google
+ # App Engine and Google Kubernetes Engine. In other environments you can
+ # configure authentication easily, either directly in your code or via
+ # environment variables. Read more about the options for connecting in the
+ # [Authentication
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
#
# If you just want to write your application's logs to the Stackdriver
# Logging service, you may find it easiest to use the [Stackdriver Logging
# Instrumentation](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/instrumentation)
@@ -345,16 +348,10 @@
#
# logging = Google::Cloud::Logging.new timeout: 120
# ```
#
module Logging
- # Initialize :error_reporting as a nested Configuration under
- # Google::Cloud if haven't already
- unless Google::Cloud.configure.option? :logging
- Google::Cloud.configure.add_options logging: :monitored_resource
- end
-
##
# Creates a new object for connecting to the Stackdriver Logging service.
# Each call creates a new connection.
#
# For more information on connecting to Google Cloud see the
@@ -373,10 +370,11 @@
# APIs](https://developers.google.com/identity/protocols/OAuth2).
#
# The default scope is:
#
# * `https://www.googleapis.com/auth/logging.admin`
+ #
# @param [Integer] timeout Default timeout to use in requests. Optional.
# @param [Hash] client_config A hash of values to override the default
# behavior of the API client. Optional.
# @param [String] project Alias for the `project_id` argument. Deprecated.
# @param [String] keyfile Alias for the `credentials` argument.
@@ -394,39 +392,90 @@
# puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
# end
#
def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil,
client_config: nil, project: nil, keyfile: nil
- project_id ||= (project || Logging::Project.default_project_id)
+ project_id ||= (project || default_project_id)
project_id = project_id.to_s # Always cast to a string
- fail ArgumentError, "project_id is missing" if project_id.empty?
+ raise ArgumentError, "project_id is missing" if project_id.empty?
- credentials ||= (keyfile || Logging::Credentials.default(scope: scope))
+ scope ||= configure.scope
+ timeout ||= configure.timeout
+ client_config ||= configure.client_config
+
+ credentials ||= (keyfile || default_credentials(scope: scope))
unless credentials.is_a? Google::Auth::Credentials
credentials = Logging::Credentials.new credentials, scope: scope
end
Logging::Project.new(
Logging::Service.new(
project_id, credentials, timeout: timeout,
- client_config: client_config))
+ client_config: client_config
+ )
+ )
end
##
# Configure the Google::Cloud::Logging::Middleware when used in a
# Rack-based application.
#
+ # The following Stackdriver Logging configuration parameters are
+ # supported:
+ #
+ # * `project_id` - (String) Project identifier for the Stackdriver
+ # Logging service you are connecting to. (The parameter `project` is
+ # considered deprecated, but may also be used.)
+ # * `credentials` - (String, Hash, Google::Auth::Credentials) The path to
+ # the keyfile as a String, the contents of the keyfile as a Hash, or a
+ # Google::Auth::Credentials object. (See {Logging::Credentials}) (The
+ # parameter `keyfile` is considered deprecated, but may also be used.)
+ # * `scope` - (String, Array<String>) The OAuth 2.0 scopes controlling
+ # the set of resources and operations that the connection can access.
+ # * `timeout` - (Integer) Default timeout to use in requests.
+ # * `client_config` - (Hash) A hash of values to override the default
+ # behavior of the API client.
+ # * `log_name` - (String) Name of the application log file. Default:
+ # `"ruby_app_log"`
+ # * `log_name_map` - (Hash) Map specific request routes to other log.
+ # Default: `{ "/_ah/health" => "ruby_health_check_log" }`
+ # * `monitored_resource.type` (String) Resource type name. See [full
+ # list](https://cloud.google.com/logging/docs/api/v2/resource-list).
+ # Self discovered on GCP.
+ # * `monitored_resource.labels` -(Hash) Resource labels. See [full
+ # list](https://cloud.google.com/logging/docs/api/v2/resource-list).
+ # Self discovered on GCP.
+ # * `labels` - (Hash) User defined labels. A `Hash` of label names to
+ # string label values or callables/`Proc` which are functions of the
+ # Rack environment.
+ #
# See the [Configuration
# Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/stackdriver/guides/instrumentation_configuration)
# for full configuration parameters.
#
- # @return [Stackdriver::Core::Configuration] The configuration object
+ # @return [Google::Cloud::Config] The configuration object
# the Google::Cloud::Logging module uses.
#
def self.configure
yield Google::Cloud.configure.logging if block_given?
Google::Cloud.configure.logging
+ end
+
+ ##
+ # @private Default project.
+ def self.default_project_id
+ Google::Cloud.configure.logging.project_id ||
+ Google::Cloud.configure.project_id ||
+ Google::Cloud.env.project_id
+ end
+
+ ##
+ # @private Default credentials.
+ def self.default_credentials scope: nil
+ Google::Cloud.configure.logging.credentials ||
+ Google::Cloud.configure.credentials ||
+ Logging::Credentials.default(scope: scope)
end
end
end
end