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

info.rb

This Ruby code snippet demonstrates the usage of a Logger object in Ruby. The first line, require 'logger', is used to load the Logger module into the current scope.

The next line, logger = Logger.new(STDOUT), creates a new Logger object and assigns it to the variable logger. The parameter STDOUT is passed to the constructor, which tells the logger to write its output to the standard output stream (i.e., the console).

The third line, puts "Level INFO", outputs a string to the console indicating that the log level has been set to INFO. The fourth line, logger.level = Logger::INFO, sets the log level of the logger to INFO. This means that only messages with a severity level of INFO or higher will be output by the logger.

The fifth line, logger.warn("Nothing to do!"), outputs a warning message to the console using the warn method. The method takes a string parameter containing the message to be output. Since the log level is set to INFO, this message will not be output because its severity level is lower than INFO.

The sixth line, logger.info("Program started"), outputs an informational message to the console using the info method. This message has a severity level of INFO, which means it will be output by the logger because the log level is set to INFO or higher.

Finally, the seventh line, logger.debug("Created logger"), outputs a debug message to the console using the debug method. Since the log level is set to INFO, this message will not be output because its severity level is lower than INFO.

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:0x00007fa2c514b5f8


puts "Level INFO"
Level INFO
#=> nil
logger.level = Logger::INFO # <= change level
#=> 1

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

Executed with Ruby 3.4.4.