Heads up: This description was created by AI and might not be 100% accurate.

warn.rb

This Ruby code snippet demonstrates how to set the logging level of a logger object in Ruby. The Logger class provides several levels of logging, including DEBUG, INFO, WARN, and ERROR. By default, the logging level is set to INFO.

In this code snippet, we first create a new instance of the Logger class and assign it to a variable named logger. We then set the logging level of the logger object to WARN using the level= method. This means that only messages with a severity level of WARN or higher will be logged.

Next, we output three log messages using the warn, info, and debug methods of the Logger class. The first message is logged at the WARN level, so it should be displayed because the logging level is set to WARN. The second message is logged at the INFO level, but since the logging level is only set to WARN, it will not be displayed. The third message is logged at the DEBUG level, but since the logging level is only set to WARN, it will also not be displayed.

Overall, this code snippet demonstrates how to set the logging level of a logger object in Ruby and how to use different methods to log messages with different severity levels.

Additional Note

Ref. https://docs.ruby-lang.org/ja/latest/library/logger.html

Ruby code snippet

require 'logger'
(irb):1: warning: logger was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 3.5.0.
You can add logger to your Gemfile or gemspec to silence this warning.
#=> true

logger = Logger.new(STDOUT)
#=> 
#<Logger:0x00007f0085d9b728


puts "Level WARN"
Level WARN
#=> nil
logger.level = Logger::WARN # <= change level
#=> 2

logger.warn("Nothing to do!") # output
W, [2025-06-27T15:34:24.432507 #2820]  WARN -- : Nothing to do!
#=> true
logger.info("Program started") # none
#=> true
logger.debug("Created logger") # none
#=> true

Executed with Ruby 3.4.4.