Let's inspect another benefit of the Logger class, the ability to log to a separate log file. Modify our script so it looks like this:

require 'logger'

logger = Logger.new('development.log')

logger.info 'user logged in'
logger.error 'could not connect to the database'

Note that instead of using $stdout, we're now passing the name of the log file, development.log. This tells the logger to log all messages to this file instead of the terminal. If the file doesn't exist, it will create one. Let's verify by running the script.

$ ruby main.rb

No logs were printed to the terminal. However, it should've created a new file called development.log in the current directory, with the following content.

# Logfile created on 2023-07-29 15:09:08 -0700 by logger.rb/v1.5.3
I, [2023-07-29T15:09:08.616862 #36690]  INFO -- : user logged in
E, [2023-07-29T15:09:08.616903 #36690] ERROR -- : could not connect to the database

If you run the program again, the logger will append the new log messages to the existing file. So you retain all the previous logs, which is pretty handy in production environment.