Class: Google::Cloud::Logging::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/google/cloud/logging/rails.rb

Overview

Railtie

Adds the Middleware to Rack in a Rails environment. The middleware will set env['rack.logger'] to a Logger instance to be used by the Rails application.

The Middleware is only added when certain conditions are met. See Railtie.use_logging? for details.

When loaded, the Middleware will be inserted before the Rails::Rack::Logger Middleware, which allows it to set the env['rack.logger'] in place of Rails's default logger. The Railtie will also initialize the logger with correct GCP project_id and keyfile if they are defined in the Rails environment.rb file as follows:

config.google_cloud.logging.project_id = "my-gcp-project"
config.google_cloud.logging.keyfile = "/path/to/secret.json"

or

config.google_cloud.project_id = "my-gcp-project"
config.google_cloud.keyfile = "/path/to/secret.json"

If omitted, project_id will be initialized with default environment variables.

Class Method Summary collapse

Class Method Details

.use_logging?(config) ⇒ Boolean

Determine whether to use Stackdriver Logging or not.

Returns true if Stackdriver Logging is enabled for this application. That is, if all of the following are true:

  • A valid GCP project_id is available, either because the application is hosted on Google Cloud or because it is set in the configuration.
  • The API is able to authenticate, again either because the application is hosted on Google Cloud or because an appropriate keyfile is provided in the configuration.
  • Either the Rails environment is set to production or the config.google_cloud.use_logging configuration is explicitly set to true.

Parameters:

  • config (Rails::Railtie::Configuration)

    The Rails.application.config

Returns:

  • (Boolean)

    Whether to use Stackdriver Logging



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/google/cloud/logging/rails.rb', line 111

def self.use_logging? config
  gcp_config = config.google_cloud
  # Return false if config.google_cloud.use_logging is explicitly false
  return false if gcp_config.key?(:use_logging) &&
                  !gcp_config.use_logging

  # Try authenticate authorize client API. Return false if unable to
  # authorize.
  keyfile = gcp_config.logging.keyfile || gcp_config.keyfile
  begin
    Google::Cloud::Logging::Credentials.credentials_with_scope keyfile
  rescue Exception => e
    warn "Google::Cloud::Logging is not activated due to " \
      "authorization error: #{e.message}\nFalling back to default " \
      "logger"
    return false
  end

  project_id = gcp_config.logging.project_id || gcp_config.project_id ||
               Google::Cloud::Logging::Project.default_project
  if project_id.to_s.empty?
    warn "Google::Cloud::Logging is not activated due to empty " \
      "project_id; falling back to default logger"
    return false
  end

  # Otherwise default to true if Rails is running in production or
  # config.google_cloud.use_logging is true
  Rails.env.production? || gcp_config.use_logging
end