Logging

Logger stdlib

Logger lives in the standard library, so no gems are needed for basic logging.

Create a logger with a destination such as STDOUT and write a message with info. Run the script and the line appears in the terminal with a timestamp and a severity label.

require "logger" log = Logger.new($stdout) log.info("server started")

Levels

Levels filter noise by importance. Set level to INFO to hide debug messages while keeping warnings and errors.

Each message method matches a level, from debug for small details to fatal for crashes that stop the program.

require "logger" log = Logger.new($stdout) log.level = Logger::INFO log.debug("hidden detail") log.warn("visible warning")

Custom formatter

The default line format is readable but often too long. Replace formatter with a proc to control every line.

The block receives severity, time, progname, and message. Return a string with a newline at the end.

require "logger" log = Logger.new($stdout) log.formatter = proc do |severity, time, _progname, msg| "#{time} #{severity}: #{msg}\n" end log.info("ready")

Rotate logs

Long running programs must not grow one file forever. Pass a rotation policy when the log points at a file.

With daily rotation Ruby keeps one file per day and starts a fresh file at midnight automatically.

require "logger" log = Logger.new("app.log", "daily") log.info("rotated daily")

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? require "logger" require "stringio" io = StringIO.new log = Logger.new(io) log.level = Logger::INFO log.formatter = proc { |severity, _time, _progname, msg| "#{severity} #{msg}\n" } log.debug("hidden detail") log.warn("visible warning") puts io.string

    Answer

    WARN visible warning

    The level hides the debug line, and the formatter prints the severity and the message for the warning.

  2. What does this program print? require "logger" puts Logger::DEBUG puts Logger::WARN

    Answer

    0 / 2

    The levels are integers from debug at 0 upward, and a message below the level is dropped.

  3. What does this program print? require "logger" puts Logger::SEV_LABEL[Logger::ERROR]

    Answer

    ERROR

    SEV_LABEL maps the level number to the label the default formatter writes.

  4. What does log.level = Logger::INFO do?

    Answer

    Hides debug messages and keeps info, warnings and errors

    The level is a threshold, so a message at or above it is written and a lower one is dropped.

  5. Which arguments does a Logger formatter proc receive?

    Answer

    Severity, time, progname and message

    The proc gets all four parts, and it returns the whole line including the newline.

  6. What does Logger.new("app.log", "daily") do?

    Answer

    Keeps one log file per day and starts a new one at the date change

    The second argument is the rotation policy, which keeps a long-running program from growing one file forever.

  7. What does Logger.new($stdout) need besides the gem itself?

    Answer

    Nothing, the default formatter and level are enough

    The destination is the only required argument, and the defaults write a timestamped line.

  8. What happens to a message below the logger level?

    Answer

    It is dropped

    The level is a filter, which is what keeps a production log free of debug noise.

  9. Why must a custom formatter end its line with a newline?

    Answer

    The logger writes the string as it is, so the newline separates the records

    The formatter owns the whole line, so the newline is part of what it returns.

  10. Which destination suits a program whose output a supervisor collects?

    Answer

    STDOUT or STDERR, so the supervisor captures it

    Writing to the standard streams keeps the log with the process output the supervisor already collects.

Article author: Arthur Isaev