warn.rb

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

This Ruby code snippet demonstrates basic usage of the Logger class. It initializes a logger that outputs to standard output (STDOUT) and then sets the logging level to WARN. Messages with severity levels of WARN and higher (e.g., ERROR, FATAL) will be printed, while INFO and DEBUG messages are suppressed because the logging level is set to WARN. The output shows only the WARN message being printed to the console, along with a timestamp and severity level.

Additional Note

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

Ruby code snippet

require 'logger'
#=> true

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


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

logger.warn("Nothing to do!") # output
W, [2025-10-11T11:55:27.814483 #2932]  WARN -- : Nothing to do!
#=> true
logger.info("Program started") # none
#=> true
logger.debug("Created logger") # none
#=> true

Executed with Ruby 3.4.7.