Should I log messages to stderr or stdout?
Asked Answered
F

3

97

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 :(

Frontogenesis answered 7/2, 2011 at 7:57 Comment(5)
If you use a language like java why not use one of the famous logging frameworks, like log4j?Salvia
I'd really recommend that you look at some logging libraries before spending the time implementing logging. If you let us know which language you are writing in, I'm sure there will be lots of suggestions.Cashmere
what language do you use? Is it c or c++?Buatti
The first four commenters seem to think that rfw is asking for help on how to write logging code. He is not. He has already made his decision and he is just wondering what the best practices are for the high-level behavior of a logging system. The language he is using is irrelevant. flash, your comment could have been more useful if you just told us whether log4j uses stderr or stdout.Sassenach
log4j outputs to stdout by default. This is an early example of the utter cluelessness now rampant in the Java world. Never mind that stderr was invented in the 1970s for this purpose. Too bad it wasn't named stdlog instead. (In C++, std::clog is usually aliased to std::cerr)Chatelaine
T
79

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.

Tasty answered 7/2, 2011 at 8:0 Comment(1)
Since it's an interpreter, I'd expect the majority of users to output something to stdout. It may have support for printing to stderr, but I guess that's their own funeral...Frontogenesis
R
16

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.

Retardation answered 7/2, 2011 at 8:6 Comment(0)
B
9

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.

Bruis answered 23/12, 2022 at 6:40 Comment(1)
+1 for the buffering consideration. Having errors be saved ASAP is important, especially if they occur rarely and thus might not be flushed for a long time otherwiseUnlive

© 2022 - 2024 — McMap. All rights reserved.