In a Rails 8.1 app, which log level does each environment run at, and how do you change production's without a code change?
answer
- framework default vs generated production.rb
- ENV.fetch with an "info" fallback
- SQL and template lines are debug
- Rails.logger.debug with a block
- log_at in a controller
basics
~10 sDevelopment and test log at debug, Rails' own default. The generated production.rb sets config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info"), so production runs at info until you set RAILS_LOG_LEVEL and restart.
solid answer
~40 s`config.log_level` defaults to `:debug` inside Rails, so development and test log everything, including each SQL statement and template render. The production.rb that `rails new` writes overrides it with `config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info")`, so production drops those debug lines and keeps the request lines: `Started`, `Processing by`, `Parameters` and `Completed`. To get more detail you set `RAILS_LOG_LEVEL=debug` in the environment and restart, because the level is applied once at boot. The variable only works because that generated line reads it. For a narrower switch, `log_at :debug, if: -> { cookies[:debug] }` in a controller lowers the level for matching requests only. Prefer `Rails.logger.debug { "..." }` with a block, so the string is never built when debug is off.
code
ruby · 9 linesclass CheckoutsController < ApplicationController
# Debug-level lines only for requests carrying the cookie
log_at :debug, if: -> { cookies[:debug_checkout] }
def create
Rails.logger.debug { "cart skus=#{@cart.line_items.map(&:sku).join(',')}" }
# ...
end
endgo deeper
Recall the six levels in order, that development logs at debug, and that the generated production.rb reads RAILS_LOG_LEVEL with an info fallback.
Explain which Rails lines need debug (SQL, template renders) and which survive at info, and why the block form of Rails.logger.debug saves work.
Show how you would get debug detail for one failing request with log_at, without restarting and without flooding production with bind values.
Weigh log volume and data exposure against diagnosability, and decide which teams may raise production verbosity, how, and for how long.
## Where the level comes from Rails keeps one application logger, `Rails.logger`, and every framework logs through it: Action Controller, Action View, Active Record, Active Job. Its threshold is **`config.log_level`**. Messages below the threshold are dropped before they are formatted. The levels, from most to least verbose, are `:debug`, `:info`, `:warn`, `:error`, `:fatal` and `:unknown`. Two places decide the value: 1. **Rails' own default.** `Rails::Application::Configuration` starts with `@log_level = :debug`. The generated `development.rb` and `test.rb` never touch it, so both environments log at **debug**. 2. **The generated `config/environments/production.rb`.** `rails new` writes this line: ```ruby # config/environments/production.rb (as generated by Rails 8.1) config.log_tags = [ :request_id ] config.logger = ActiveSupport::TaggedLogging.logger(STDOUT) # Change to "debug" to log everything (including potentially personally-identifiable information!). config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info") ``` So production logs at **info** unless the environment says otherwise. The variable is not a framework feature: no other part of Rails reads `RAILS_LOG_LEVEL`. Delete that line from `production.rb` and the variable does nothing, and production falls back to Rails' default of debug. During boot, the `initialize_logger` initializer applies the level and wraps the logger in an `ActiveSupport::BroadcastLogger`. That happens once. A new value in the environment needs a **restart**, or a redeploy on a container platform. The generated `config/deploy.yml` has a commented-out `RAILS_LOG_LEVEL: debug` entry under `env`, so the switch already exists there. ## What each level shows in a Rails log Every Rails log subscriber declares the level its lines need. That decides what you lose when you raise the threshold: | Line | Source | Needs | |---|---|---| | `Started POST "/checkouts" for ...` | `Rails::Rack::Logger` | info | | `Processing by CheckoutsController#create as HTML` and `Parameters:` | Action Controller | info | | `Rendered layout layouts/application.html.erb` | Action View | info | | `Rendered checkouts/new.html.erb within layouts/application` and partial lines | Action View | debug | | `Order Load (2.1ms) SELECT ...` with its binds | Active Record | debug | | `Completed 200 OK in 812ms (...)` | Action Controller | info | At info you can still see which action ran, its parameters, its status and its timing summary. What you lose is each SQL statement and each template render. Those are what a slow checkout makes you want, which is why people reach for debug in production. The generated comment names the cost: at debug, SQL lines include bind values, so data such as email addresses reaches the log. Only bind values whose column matches `filter_attributes` are masked. ## Narrower tools than a global switch - **Block form.** `Rails.logger.debug { "cart=#{cart.line_items.map(&:sku)}" }` runs the block only when debug is enabled. The plain-string form builds the string first and then throws it away at info. - **Per request.** `ActionController::Logging` adds `log_at`, which wraps actions in an `around_action` that sets a **thread-local** level. Only the matching request logs at debug, for example `log_at :debug, if: -> { cookies[:debug] }`. - **Quieting a block.** `Rails.logger.silence(Logger::ERROR) { ... }` raises the threshold for the block. `ActiveSupport::LoggerSilence` provides it. - **Asking first.** `Rails.logger.debug?` and `info?` report whether a level is enabled, which helps you skip expensive work. ## Turning production up during an incident When a bug only shows up in production and the info lines are not enough, the usual sequence is: 1. Set `RAILS_LOG_LEVEL=debug` where the platform defines the app's environment, for example the `env` section of `config/deploy.yml`. 2. Restart or redeploy, so `initialize_logger` applies the new level at boot. 3. Reproduce the failing checkout and collect its lines, ideally by request id. 4. Remove the variable and restart again, so production goes back to info. Both strings and symbols work as values. Boot upcases whatever you pass and looks up the matching `Logger` constant, so `"debug"` and `:debug` are equivalent. A misspelled level fails at boot instead of being silently ignored, because the constant lookup raises `NameError`. ## Common mistakes - Believing Rails itself defaults production to info. The generated file does that, not the framework. - Changing the variable on a running server and expecting a change without a restart. - Leaving production at debug for days after an incident. The log fills with bind values and volume. - Using `puts` in a controller. Its output skips the level, the request tags and the formatter. ## Takeaway Debug is Rails' default. The generated production.rb is what moves production to info, and it reads `RAILS_LOG_LEVEL` to let you move it back without a code change. When only one request or one block needs more detail, `log_at`, the block form and `silence` are better tools than a global switch.
- Why does `Rails.logger.debug "total=#{expensive_total}"` still cost time in production at info?Ruby evaluates the argument before calling `debug`, so `expensive_total` runs and the string is built before the logger sees that debug is off. With the block form, `Rails.logger.debug { "total=#{expensive_total}" }`, the logger checks its level first and calls the block only when debug is enabled. Rails' `BroadcastLogger` also makes sure the block runs once, however many sinks are attached.
- Production must show debug output for one customer's checkout only. What does Rails give you short of RAILS_LOG_LEVEL?`log_at` in the controller, for example `log_at :debug, if: -> { cookies[:debug_checkout] }`. It adds an `around_action` that calls `logger.log_at(:debug)`, which sets a thread-local level for that request. Other requests stay at info and nothing restarts. The condition is yours to protect: a cookie anyone can set lets anyone turn on verbose logs.
- Someone deleted the log_level line from production.rb. What level does production run at now?Debug, because `config.log_level` falls back to Rails' own default of `:debug`. Setting `RAILS_LOG_LEVEL` no longer helps either, because nothing else reads that variable. Production would start logging every SQL statement with its bind values, so restore the line or set `config.log_level` explicitly.
- Rails reads RAILS_LOG_LEVEL itself, so the production.rb line is optional.
- Rails defaults every environment to info; debug has to be switched on.
- Setting RAILS_LOG_LEVEL on a running server takes effect on the next request.
- At info, Rails still logs each SQL statement, just without bind values.
- Rails.logger.debug with a string argument costs nothing when the level is info.
- log_at :debug changes config.log_level for the whole app until restart.