user193476
user193476

Reputation:

Should I log messages to stderr or stdout?

I have a program I'm writing that I want to write a custom logging facility for (e.g. diagnostic, notice, warning, error).

Should I be using the stdout or the stderr stream to do this? It is an interpreter of sorts and the user can ask it to print output.

Edit: Please stop recommending me logging frameworks :(

Upvotes: 103

Views: 30725

Answers (3)

Paul Draper
Paul Draper

Reputation: 83393

Stdout is the "output" or "result" of a program. (This is why shell piping applies to stdout.)

Stderr is the "logging" or "diagnostics" of a program. (This is why stderr is usually unbuffered.)

It sounds like these messages would be the latter.

Upvotes: 15

Andrey Taptunov
Andrey Taptunov

Reputation: 9515

Do you write something else to stdout? If the answer is Yes then it would be quite hard for someone to distinguish between normal flow and errors in the middle of the file that was used to redirect from stdout.

What if stdout will be used as input for another program? Will it be able to parse this debug information? In this case it would be good to write only critical errors to stderr. If you need to write something else you may consider to write to additional log file.

Upvotes: 18

Jakob
Jakob

Reputation: 24370

Regular output (the actual result of running the program) should go on stdout, things like you mentioned (e.g. diagnostic, notice, warning, error) on stderr.

If there is no "regular output", I would say that it doesn't really matter which one you choose. You could argue that the logging is the only output, so that should go to stdout. Or you could argue that it is still "exceptional information" which should go to stderr.

Upvotes: 83

Related Questions