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 loaded only when certain conditions are met. These conditions are when the configuration Google::Cloud.configure.use_logging (also available as Rails.application.config.google_cloud.use_logging for a Rails application) is set to true, or, if the configuration is left unset but Rails.env.production? is true.

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. See the Configuration Guide on how to configure the Railtie and Middleware.

Class Method Summary collapse

Class Method Details

.set_default_loggerObject

This should be called once the application determines that it is safe to start background threads and open gRPC connections. It informs the middleware system that it is safe to use Google Cloud Logging. This is called during Rails initialization when the set_default_logger_on_rails_init configuration is set.

Generally, this matters if the application forks worker processes; this method should be called only after workers are forked, since threads and network connections interact badly with fork. For example, when running Puma in clustered mode, this method should be called in an on_worker_boot block.

If the application does no forking, this method can be called any time early in the application initialization process. Or by setting the set_default_logger_on_rails_init configuration.

If the set_default_logger_on_rails_init configuration is not set, and set_default_logger is not called in a post-fork hook, the default Rails logger object will not be set to use the Google Cloud Logging Logger object. For best results, an application should call this method at the appropriate time, such as a post-fork hook.



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/google/cloud/logging/rails.rb', line 115

def self.set_default_logger
  return if Middleware.logger.nil?
  return if Rails.logger.is_a? Google::Cloud::Logging::Logger

  # configure the Middleware logger to use the same settings as Rails
  Middleware.logger.level = Rails.logger.level
  # TODO: are there more settings to be set here?

  # Replace the Rails default logger
  Rails.application.config.logger = Middleware.logger
  Rails.logger = Middleware.logger
end