Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

How to set rack environment variable from inside Rails

rack-cache gem relies on @env['rack.errors'] setting to log error messages:

    78:       # write log message to rack.errors
    79:       if verbose?
    80:         binding.pry
    81:         message = "cache: [%s %s] %s\n" %
    82:           [@request.request_method, @request.fullpath, trace]
 => 83:         @env['rack.errors'].write(message)
    84:       end

It is currently set to @env['rack.errors'] #⇒ #<IO:<STDERR>>.

I need to change it to use Rails.logger. The obvious opportunity is to hack into rack-cache initializer RAILS_CACHE.logger = .... I wonder whether there is a common way to access rack environment from Rails, like (pseudocode):

Rails.RACK_ENV['rack.errors'] = Rails.logger

Upvotes: 0

Views: 1356

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176402

The Rack environment is only accessible within the context of a request, hence in a controller or view.

To access the environment you can use

request.env['whatever']

Be careful when you modify the Rack environment as other pieces of the Rails stack may rely on it.

Upvotes: 1

Related Questions