Logging

Contents
Logger stdlib
Levels
Custom formatter
Rotate logs

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")

Next: CSV

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: