Logging in depth

Logger and levels

Logger has a level and drops everything below it. The level is the switch that keeps a development log loud and a production log quiet without editing a call:

require "logger" logger = Logger.new($stderr) logger.level = Logger::INFO logger.debug("hidden") logger.info("started")

The order is DEBUG, INFO, WARN, ERROR, FATAL. A line at DEBUG is free to be verbose because the level above keeps it out of production, and the one call site does not change between environments.

A formatter

The default line is a timestamp, a severity, the process id and the message. A formatter is a proc that returns the whole line, which is where a project decides what its logs look like:

require "logger" logger = Logger.new($stderr) logger.formatter = proc do |severity, time, _progname, msg| "#{time.utc.strftime("%Y-%m-%dT%H:%M:%SZ")} #{severity} #{msg}\n" end

The trailing newline is required: the formatter returns the complete line. UTC timestamps make two servers in different zones comparable, and a fixed format is what a log search can parse.

One JSON object per line

A structured event is a hash, encoded as one JSON line. A log collector reads the fields without a regex, and a new field does not break the old query:

require "logger" require "json" logger = Logger.new($stderr) logger.formatter = proc do |severity, time, _progname, msg| "#{time.utc.strftime("%Y-%m-%dT%H:%M:%SZ")} #{severity} #{msg}\n" end logger.info(JSON.generate(event: "request", path: "/", status: 200, ms: 12)) logger.warn("slow request")

ruby app.rb 2> app.log

cat app.log

2026-11-05T00:50:06Z INFO started 2026-11-05T00:50:06Z INFO {"event":"request","path":"/","status":200,"ms":12} 2026-11-05T00:50:06Z WARN slow request

The debug line is absent because the level is INFO. Mixing a sentence and a JSON object in one file is honest for a small script; a service that a collector reads logs JSON only, so every line has the same shape.

Rotation

A log that grows forever fills the disk. Logger.new takes a path, a number of old files and a size in bytes, and rotates when the current file passes it:

require "logger" logger = Logger.new("app.log", 2, 1024 * 1024) logger.info("rotated when the file passes a megabyte")

That keeps app.log plus two old files of a megabyte each. In a container the usual answer is the opposite: log to stdout, let the platform collect it, and rotate nothing inside the process.

What to watch for

A log line that contains a password, a token or a full request body is a leak that outlives the bug. Log the id and the shape, not the secret; lesson 7.5 has the warning side of the same idea.

puts in a library writes to the caller's stdout and bypasses the level. A gem logs through a Logger it was given or Logger.new($stderr), never through puts.

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 logger = Logger.new(io) logger.level = Logger::WARN logger.info("started") logger.warn("slow") puts io.string.lines.size

    Answer

    1

    The level WARN drops the info line and writes only the warning, so one line reaches the buffer.

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

    Answer

    true

    The levels are ordered integers, and an error is more severe than a warning.

  3. What does this program print? require "json" puts JSON.generate(event: "request", status: 200)

    Answer

    {"event":"request","status":200}

    The hash becomes one compact JSON object, which is the shape a collector reads without a regex.

  4. What does this program print? require "logger" require "stringio" io = StringIO.new logger = Logger.new(io) logger.formatter = proc { |severity, _time, _progname, msg| "#{severity} #{msg}\n" } logger.info("started") print io.string

    Answer

    INFO started

    The formatter owns the line, and this one writes the severity and the message without the default fields.

  5. Which line does the default Logger formatter write?

    Answer

    A timestamp, a severity, the process id and the message

    The default carries the four parts, which is why a project that needs another shape replaces the formatter.

  6. What do the second and third arguments of Logger.new("app.log", 2, 1024 * 1024) mean?

    Answer

    The number of old files and the size in bytes

    The logger keeps two old files and rotates when the current one passes a megabyte.

  7. Why use UTC in a log timestamp?

    Answer

    It makes entries from servers in different zones comparable

    One time zone for every host is what lets a search order two servers' lines.

  8. Why does a library log through a Logger instead of puts?

    Answer

    puts writes to the caller's stdout and bypasses the level

    A level and a destination belong to the caller, so a library that prints directly takes both decisions away.

  9. Why is a password in a log line worse than a password in a request?

    Answer

    The log outlives the request and is copied to other systems

    The leak survives the bug that caused it, which is why the page logs the id and the shape instead.

  10. What does a service that a collector reads usually write?

    Answer

    One JSON object per line, so every line has the same shape

    A uniform shape is what lets a field query work on every line, old and new.

Article author: Arthur Isaev