Class: Google::Cloud::Logging::Railtie
- Inherits:
-
Rails::Railtie
- Object
- Rails::Railtie
- Google::Cloud::Logging::Railtie
- Defined in:
- lib/google/cloud/logging/rails.rb
Overview
Railtie
Google::Cloud::Logging::Railtie automatically add the Google::Cloud::Logging::Middleware to Rack in a Rails environment. The middleware will set env['rack.logger'] to a Google::Cloud::Logging::Logger instance to be used by the Rails application.
The Middleware is only added when certain conditions are met. See Railtie.use_logging? for detail.
When loaded, the Google::Cloud::Logging::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 should also initialize the logger with correct GCP project_id and keyfile if they are defined in Rails environment.rb as follow: 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
-
.use_logging?(config) ⇒ Boolean
Determine whether to use Stackdriver Logging or not.
Class Method Details
.use_logging?(config) ⇒ Boolean
Determine whether to use Stackdriver Logging or not.
Returns true if valid GCP project_id is provided and underneath API is able to authenticate. Also either Rails needs to be in "production" environment or config.stackdriver.use_logging is explicitly true.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/google/cloud/logging/rails.rb', line 94 def self.use_logging? config gcp_config = config.google_cloud # Return false if config.stackdriver.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.}\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.stackdriver.use_logging is true Rails.env.production? || gcp_config.use_logging end |