Change the logging level in Ruby

Set a Logger severity threshold, restore temporary debugging settings, and validate a Rails log-level environment variable.

Best practices
Illustration of monitoring and troubleshooting application behavior

Set logger.level to the minimum severity you want to emit. Ruby's Logger filters out messages below that threshold; the setting belongs to that logger instance.

Set a threshold

require 'logger'

logger = Logger.new($stdout)
logger.level = Logger::WARN
logger.debug('Filtered out')
logger.warn('Visible warning')
logger.unknown('Visible unknown-severity event')

The order is DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN. A WARN threshold therefore includes UNKNOWN as well as warnings, errors, and fatal messages. Current Logger versions accept supported severity names as strings or symbols, too, such as logger.level = 'info'.

Restore a temporary setting

previous_level = logger.level
begin
  logger.level = Logger::DEBUG
  logger.debug { 'Diagnostic detail' }
  # Run the operation being investigated.
ensure
  logger.level = previous_level
end

The block form avoids generating the message when it is filtered out. Changing a shared logger's level affects other code using that instance, so the example is not an isolated per-request override in a concurrent server.

Configure Rails

Set the environment's baseline in config/environments/production.rb:

config.log_level = :info

For a deployment setting, validate the value before converting it:

level = ENV.fetch('LOG_LEVEL', 'info').downcase
allowed = %w[debug info warn error fatal unknown]
raise ArgumentError, 'Invalid LOG_LEVEL' unless allowed.include?(level)
config.log_level = level.to_sym

An empty environment value is not the same as a missing variable; this example rejects it instead of silently choosing a level.

Common questions

Use separate Logger instances for modules that need independent thresholds, and manage their output destinations deliberately. Rails applications can use a logging library with more advanced per-component controls, but configuration depends on that library.

If you must map an external string to a constant, use an allowlist rather than arbitrary const_get input. Lowering the threshold only affects future log calls; it cannot recover previously filtered messages.

References: Ruby Logger and Rails logging configuration.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting