Using the Logger Library - How to Write Log Messages in Ruby

Woman and man working in large open modern office

Erik Von Weber/Stone/Getty Images

Using the logger library in Ruby is an easy way to keep track of when something has gone wrong with your code. When something goes wrong, having a detailed account of exactly what happened leading up to the error can save you hours in locating the bug. As your programs get larger and more complex, you may want to add a way to write log messages. Ruby comes with a number of useful classes and libraries called the standard library. Among these is the logger library, which provides prioritized and rotated logging.

Basic Usage

Since the logger library comes with Ruby, there's no need to install any gems or other libraries. To begin using the logger library, simply require 'logger' and create a new Logger object. Any messages written to the Logger object will be written to the log file.

#!/usr/bin/env ruby
require 'logger'
log = Logger.new('log.txt')
log.debug "Log file created"

Priorities

Each log message has a priority. These priorities make it simple to search log files for serious messages, as well as have the logger object automatically filter out lesser messages when they're not needed. You can think of it sort of like your To Do list for the day. Some things absolutely must be done, some things really should get done, and some things can be put off until you have time to do them.

In the previous example, the priority was debug, the least important of all the priorities (the "put off until you have time" of your To Do list, if you will). The log message priorities, in order from least to most important, are as follows: debug, info, warn, error, and fatal. To set the level of messages the logger should ignore, use the level attribute.

#!/usr/bin/env ruby
require 'logger'
log = Logger.new('log.txt')
log.level = Logger::WARN
log.debug "This will be ignored"
log.error "This will not be ignored"

You can create as many log messages as you want and you can log every tiny little thing your program does, which makes priorities extremely useful. When you're running your program, you can leave the logger level on something like warn or error to catch the important stuff. Then, when something goes wrong, you can lower the logger level (either in the source code or with a command-line switch) to get more information.

Rotation

The logger library also supports log rotation. Log rotation keeps logs from getting too large and helps in searching through older logs. When log rotation is enabled and the log reaches either a certain size or a certain age, the logger library will rename that file and create a fresh log file. Older log files can also be configured to be deleted (or "fall out of rotation") after a certain age.

To enable log rotation, pass 'monthly', 'weekly', or 'daily' to the Logger constructor. Optionally, you can pass a maximum file size and number of files to keep in rotation to the constructor.

#!/usr/bin/env ruby
require 'logger'
log = Logger.new( 'log.txt', 'daily' )
log.debug "Once the log becomes at least one"
log.debug "day old, it will be renamed and a"
log.debug "new log.txt file will be created."
Format
mla apa chicago
Your Citation
Morin, Michael. "Using the Logger Library - How to Write Log Messages in Ruby." ThoughtCo, Feb. 16, 2021, thoughtco.com/write-log-messages-in-ruby-2908323. Morin, Michael. (2021, February 16). Using the Logger Library - How to Write Log Messages in Ruby. Retrieved from https://www.thoughtco.com/write-log-messages-in-ruby-2908323 Morin, Michael. "Using the Logger Library - How to Write Log Messages in Ruby." ThoughtCo. https://www.thoughtco.com/write-log-messages-in-ruby-2908323 (accessed March 19, 2024).