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



110
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
141
142
# File 'lib/google/cloud/logging/rails.rb', line 110

def self.use_logging? config
  logging_config = Railtie.parse_rails_config config

  # Return false if config.google_cloud.use_logging is explicitly false
  use_logging = logging_config[:use_logging]
  return false if use_logging == false

  project_id = logging_config[:project_id] ||
               Google::Cloud::Logging::Project.default_project
  keyfile = logging_config[:keyfile]

  # Try authenticate authorize client API. Return false if unable to
  # authorize.
  begin
    Google::Cloud::Logging::Credentials.credentials_with_scope keyfile
  rescue Exception => e
    STDOUT.puts "Note: Google::Cloud::Logging is disabled because " \
      "it failed to authorize with the service. (#{e.message}) " \
      "Falling back to the default Rails logger."
    return false
  end

  if project_id.to_s.empty?
    STDOUT.puts "Note: Google::Cloud::Logging is disabled because " \
      "the project ID could not be determined. " \
      "Falling back to the default Rails 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? || use_logging
end