In the following Ruby code, should logger default to `$stderr` or `$stdout`?

logger =

CSV do |csv| "Generating row..."
csv << ['1', '2', '3']

Please reply with your reasoning.

@ioquatix I prefer systemd’s log to stdout with level as the first char. Makes no sense to have a diff stream for one level.

@mperham I'm not sure I follow you. Are you talking about a daemon spawned by systemd? w.r.t. different streams, are you thinking `log.debug` goes to stdout and `log.error` goes to stderr?

@ioquatix stderr is reserved for errors emitted by the program. Outputting to stdout allows your program to be piped into other programs, while keeping any error messages separate from stdout.

@postmodern Do you want to pipe logging output into the subsequent program?

@ioquatix yes, it should be allowed. grep, tee, logger, come to mind as common log pipping programs.

@ioquatix @postmodern yes but I’m speaking fir long running daemons, not Unix style scripts.

@mperham @postmodern Why does it make any difference for long running daemons vs scripts? Is there some downside to putting all logging/diagnostics to `stderr`? In contrast, since `stdout` is buffered, there are downsides to using it for logging, e.g. missing output on crash.

@ioquatix my first thought is that "info" isn't an error.

@phaedryx stderr is for diagnostics. Do you want to mix up info logger output in your CSV?

@ioquatix I guess I assume diagnostics are about figuring out what went wrong. I tend to think of "info" as "a thing happened, FYI"

@ioquatix what if there weren't a default and you had to explicitly choose:

logger =
logger =

@phaedryx Right now there isn't any default. I wanted to propose to default to `stderr` because that's in line with POSIX standards, but there was a lot of push back, but I don't understand why. So, that's why I'm asking.

@ioquatix This is interesting because it isn't something I think much about. I've searched around and this is a point of contention several places; especially depending on how you define "diagnostic information".

Some links I found:

My current thinking is that "standard out" is for the standard (expected/typical) output? It is certainly more nuanced, though, than my initial reaction.

@phaedryx Is it normal output of the program (i.e. results)? Then it belongs on `stdout`. Otherwise it belongs on `stderr`. The original reason for `stderr` is because people used to print documents by piping the output into the printer device. Once the errors started coming out on the printer, apparently a few days later `stderr` was born :p

@ioquatix There's a good explanation why it should go to stderr in "A Research UNIX Reader: Annotated Excerpts from the Programmer’s Manual":

@phil_pirozhkov I personally agree it should go to `stderr`. `stdout` is for normal program output, `stderr` is for everything else. Logging is not, by definition, normal program output, but a sequence of events that occurred in the program, and is typically used for debugging and diagnosing issues with the program.

Sign in to participate in the conversation

A Mastodon instance for Rubyists & friends