warn, warnings and log levels

warn writes to stderr

puts is the program's output and warn is the program's complaint. They go to different streams, which is why a warning does not corrupt a report a script is printing:

ruby -e 'warn "careful"; puts "ok"'

careful
ok

Both lines appear in a terminal because both streams point at it. Split them and the difference is clear:

ruby -e 'warn "careful"; puts "ok"' 2>/dev/null

ok

warn adds a newline and returns nil. $stderr.puts does the same thing with the stream named, and STDERR is the constant version. A warning that a reader should see even when stdout is piped into a file belongs on stderr.

ruby -w

The interpreter has its own warnings, and -w turns the full set on. An assigned variable that is never read is the smallest example:

x = 1

ruby -w w.rb

w.rb:1: warning: assigned but unused variable - x

The warning names the file and the line, which is what makes a warning worth reading. A run of a script with -w is a cheap lint pass: it finds a shadowed variable, a redefined method, an unused argument and a constant assigned twice.

ruby -W0 turns them off, and -W:deprecated sets one category. Warning.warn is the hook a library overrides to route interpreter warnings into its own log:

ruby -e 'Warning.warn("note\n"); puts "after"'

note
after

Logger and levels

A long-running program wants more than a line on stderr: a level, a timestamp and a way to silence the noise. The standard library has Logger for exactly that:

require "logger" formatter = ->(severity, _time, _progname, msg) { "#{severity} -- : #{msg}\n" } log = Logger.new($stderr, formatter: formatter) log.level = Logger::WARN log.info("hidden") log.warn("careful")

ruby logger_demo.rb

WARN -- : careful

The info line never appears because the level is set to WARN. The default format adds a timestamp and the process id, and both change on every run; the formatter here keeps the level and the message, so the line is stable. The order is DEBUG, INFO, WARN, ERROR, FATAL; set the level from the environment (ENV.fetch("LOG_LEVEL", "INFO")) so the same build is quiet in production and loud on a laptop. Lesson 11.3, later in the course, covers the configuration side, and lesson 11.10 goes deeper into formatters and rotation.

Warning or exception

An exception means the operation cannot continue: the caller must do something. A warning means the operation continues and the reader should know: a deprecated option, a value that was clamped, a fallback that was used. The test is whether the program has a correct answer to give.

What to watch for

A warning that fires on every request is noise, and noise is ignored. Log it once, at the point where the configuration is read, or raise the level so it does not print in production.

puts in a library is a bug: it writes to the caller's stdout. A gem writes to its logger, and the application decides where that goes.

Questions

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

  1. Which stream does warn write to?

    Answer

    The warning stream, which is stderr

    That is why a warning does not corrupt a report the script prints on stdout.

  2. What does this command print to the standard output? ruby -e 'warn "careful"; puts "ok"' 2>/dev/null

    Answer

    ok

    The warning goes to stderr, and 2>/dev/null throws that stream away.

  3. What does ruby -w add to a run?

    Answer

    The interpreter's warnings: unused variables, shadowed names, redefined methods

    It is a cheap lint pass that names the file and the line.

  4. What does this program print? require "logger" formatter = ->(severity, _time, _progname, msg) { "#{severity} -- : #{msg}\n" } log = Logger.new($stderr, formatter: formatter) log.level = Logger::WARN log.info("hidden") log.warn("careful")

    Answer

    WARN -- : careful

    The level WARN drops the info line, and the formatter prints the level and the message.

  5. Which order do the log levels follow, from quietest to loudest?

    Answer

    DEBUG, INFO, WARN, ERROR, FATAL

    A level prints itself and everything above it.

  6. What does this program print? require "logger" log = Logger.new($stdout) log.level = Logger::ERROR log.warn("careful") puts "done"

    Answer

    done

    A WARN line is below the ERROR level, so only the puts line appears.

  7. What is Warning.warn for?

    Answer

    A hook a library overrides to route interpreter warnings into its own log

    It is how a library takes the warnings Ruby prints and sends them somewhere useful.

  8. When should a problem be a warning rather than an exception?

    Answer

    When the program has a correct answer to give and the reader should know

    The test is whether the operation can continue with a value.

  9. What does this program write to the standard output? $stderr.puts "e" puts "o"

    Answer

    o

    $stderr.puts is the named form of warn, so only the puts line reaches stdout.

  10. Why is puts in a library a bug?

    Answer

    It writes to the caller's stdout instead of the caller's logger

    A gem writes to a logger, and the application decides where that goes.

Article author: Arthur Isaev